R Programming Lab 2
R Programming Lab 2
R Programming Lab
Assignment 1
Vectors and List
2. The numbers below are the first ten days of rainfall amounts in 1996. Read
them into a vector using the c() function.
0.1, 0.6, 33.8, 1.9, 9.6, 4.3, 33.7, 0.3, 0.0, 0.1
a. What was the mean rainfall? How about the standard deviation?
b. Calculate the cumulative rainfall (’running total’) over these ten days.
Confirm that the last value of the vector that this produces is equal to
the total sum of the rainfall.
c. Which day saw the highest rainfall (which.max() function)?
3. Read in a vector that contains "A", "B", "C" and "D" (use the c() function).
Using rep, produce this:
a. "A" "A" "A" "B" "B" "B" "C" "C" "C" "D" "D" "D"
b. "A" "B" "C" "D" "A" "B" "C" "D" "A" "B" "C" "D"
8. Find all elements of a given list that are not in another given list.
l1 = "x", "y", "z"
l2 = "X", "Y", "Z", "x", "y", "z"
10. Create a list containing a sequence of 15 capital letters, starting with ‘E’.
(Hint: use match() function)
14 Jan 2022, Saturday
R Programming Lab
Assignment 2 with Concepts
Vectors and List Cont.
3. Create a vector
v <- c(10,20,30,40,50,60,70,80,90,100)
a. is.vector(v)
b. length(v)
c. typeof(v) #R will save any number that you type in R as a double.
d. v[1:3]
e. v[8:10]
f. head(v, n = 3)
g. tail(v, n = 3)
4. Coercion: If you try to put multiple types of data into a vector, R will convert
the elements to a single type of data.
● If a character string is present in an atomic vector, R will convert
everything else in the vector to character strings.
● If a vector only contains logicals and numbers, R will convert the
logicals to numbers; every TRUE becomes a 1, and every FALSE
becomes a 0.
i. person[[c(1, 2)]]
j. person[[c("name", "last_name")]]
9. The following object demo is a list with information about two persons, Frank
and Petra.
demo <- list(
"Petra" = list(location = "Birmingham", kids = NULL, job = "Programmer"),
"Frank" = list(location = "Kufstein", kids = c("Peter", "Paul"))
)
a. How do we get Franks location?
b. Try demo["Frank"]$location (will return NULL). Why doesn’t this work?
c. How many kids does Frank have?
d. Our friend Petra moves from Birmingham to Vienna. Change her location
(inside demo) to Vienna.
14 Jan 2022, Saturday
R Programming Lab
Assignment 3 with Concepts
Matrices
4. Try to create the following matrices. To do so, we need to specify data, the
number of rows, and the number of columns.
a. A matrix of dimension 5X5 which contains 5L (integer) everywhere.
b. A matrix of dimension 10X1 which contains -100 (numeric)
everywhere.
c. Check that the class of your result is c("matrix", "array")
a. x+2
b. x*2
c. x/2
d. x^2
a. Compute x*y
a. x + y
b. x * y
c. x / y
9. Create a matrix x
x <- matrix(c( 1, 2, 3, 4), ncol = 2, nrow = 2)
# Combine
z <- cbind(x, y)
z <- rbind(x, y)
a. 4 and 4
b. 8 and 4
c. 4 and 8
d. 4 and 1
e. 5 and 3
f. 9 and 10
a. cbind(x1, x2)
b. rbind(x1, x2)
15. Hands on matrix subsetting. Try to answer the questions based on the
following numeric matrix mat.
mat <- matrix(c(270, 100, 330, 340, 260, 160, 10, 310, 80, 50, 60, 190, 150,
110, 290, 220, 10, 350, 100, 0), nrow = 5)