Warning: Parameter 2 to wp_hide_post_Public::query_posts_join() expected to be a reference, value given in /home/rexercis/public_html/wp-includes/class-wp-hook.php on line 286
Tag: list - R-exercises

List Vol.2 Exercises

Answers to the exercises are available here.

Exercise 1

Consider 3 vectors, day, month and year:

year=c(2005:2016)
month=c(1:12)
day=c(1:31)

Define a list Date such as:

Date=

$year
[1] 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016

$month
[1] 1 2 3 4 5 6 7 8 9 10 11 12

$day
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

Exercise 2

write an R statement that will replace the values of year element in Date list for c(2000:2010).

Exercise 3

write an R statement that will delete the value 4 of the month component of the list Date.

Learn more about lists in the online courses:

Exercise 4

Consider a vector x such that:
x=c(1,3,4,7,11,18,29)
Write an R statement that will return a list X2 with components of value:
x*2,x/2,sqrt(x) and names "x*2","x/2","sqrt(x)"

Exercise 5

Consider the X2 list.
Write an R statement that will return a vector:
2.000000 2.645751 3.316625

Exercise 6

Write an R statement that will return a concatenation M, of Date and X2 lists.

M
$year
[1] 2000 2001 2002 2003 2004 2005 2006 2007 2008

$month
[1] 1 2 3 5 6 7 8 9 10 11 12

$day
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
[24] 24 25 26 27 28 29 30 31

$`x*2`
[1] 2 6 8 14 22 36 58

$`x/2`
[1] 0.5 1.5 2.0 3.5 5.5 9.0 14.5

$`sqrt(x)`
[1] 1.000000 1.732051 2.000000 2.645751 3.316625 4.242641 5.385165

Exercise 7

Write an R statement that will return a sublist N of M, with components year,x*2 and day.

Exercise 8

Consider the N list.
Write an R statement that will return:
-the length of x*2 vector in N
-the value of the second element of vector year in N

Exercise 9

Consider 3 letters vectors, and 2 numeric vectors:
A=letters[1:4],B=letters[5:10],C=letters[11:15]
D=c(1:10),E=c(20:5)

Define a list z, with elementes x and y,such that
x is a list with elements A,B,and C;
and y is alist with elements D and E.

Exercise 10
Write an R statement that will return:
-the number on third position on the second vector of the first list of z
-the letter on fifth position on the third vector of the second list of z

Want some extra practice with lists? Please take a look here




List exercises

In the exercises below we cover the basics of lists. Before proceeding, first read section 6.1-6.2 of An Introduction to R, and the help pages for the sum, length, strsplit, and setdiff functions.

Answers to the exercises are available here.

Exercise 1
If:
p <- c(2,7,8), q <- c("A", "B", "C") and
x <- list(p, q),
then what is the value of x[2]?
a. NULL
b. "A" "B" "C"
c. "7"

Exercise 2
If:
w <- c(2, 7, 8)
v <- c("A", "B", "C")
x <- list(w, v),
then which R statement will replace "A" in x with "K".
a. x[[2]] <- "K"
b. x[[2]][1] <- "K"
c. x[[1]][2] <- "K"

Exercise 3
If a <- list ("x"=5, "y"=10, "z"=15), which R statement will give the sum of all elements in a?
a. sum(a)
b. sum(list(a))
c. sum(unlist(a))

Exercise 4
If Newlist <- list(a=1:10, b="Good morning", c="Hi"), write an R statement that will add 1 to each element of the first vector in Newlist.

Exercise 5
If b <- list(a=1:10, c="Hello", d="AA"), write an R expression that will give all elements, except the second, of the first vector of b.

Exercise 6
Let x <- list(a=5:10, c="Hello", d="AA"), write an R statement to add a new item z = "NewItem" to the list x.

Exercise 7
Consider y <- list("a", "b", "c"), write an R statement that will assign new names "one", "two" and "three" to the elements of y.

Exercise 8
If x <- list(y=1:10, t="Hello", f="TT", r=5:20), write an R statement that will give the length of vector r of x.

Exercise 9
Let string <- "Grand Opening", write an R statement to split this string into two and return the following output:

[[1]]
[1] "Grand"

[[2]]
[1] "Opening"

Exercise 10
Let:
y <- list("a", "b", "c") and
q <- list("A", "B", "C", "a", "b", "c").
Write an R statement that will return all elements of q that are not in y, with the following result:

[[1]]
[1] "A"

[[2]]
[1] "B"

[[3]]
[1] "C"

Want some extra practice with lists? Please take a look here