List exercises

[This article was first published on R-exercises, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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 and
x ,
then what is the value of x[2]?
a. NULL
b. “A” “B” “C”
c. “7”

Exercise 2
If:
w
v
x ,
then which R statement will replace “A” in x with “K”.
a. x[[2]]
b. x[[2]][1]
c. x[[1]][2]

Exercise 3
If a , 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 , write an R statement that will add 1 to each element of the first vector in Newlist.

Exercise 5
If b , write an R expression that will give all elements, except the second, of the first vector of b.

Exercise 6
Let x , write an R statement to add a new item z = “NewItem” to the list x.

Exercise 7
Consider y , write an R statement that will assign new names “one”, “two” and “three” to the elements of y.

Exercise 8
If x , write an R statement that will give the length of vector r of x.

Exercise 9
Let string , 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 and
q .
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"

To leave a comment for the author, please follow the link and comment on their blog: R-exercises.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)