Subsetting Flashcards

1
Q

[

A

Always returns an object of the same clas

Can be used to select more than one element

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

[[

A

Used to extract elements of a list or a data frame

Class not necessarily the same as the initial object

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

$

A

Used to extract elements by name

Semantics similar to that of [[]]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How do I return the third element of vector x?

A

x[3]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do I return the 4th and 5th elements of vector x?

A

x[4:5]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How do I return the 3rd and 7th elements of vector y?

A

y[c(3,7)]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do I return all elements greater than 4 in the vector t?

A

t[t > 4]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do I create a logical vector from x, given the condition: x == ‘c’?

A

u <- x == ‘c’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do I extract the first row from the matrix m?

A

m[1,]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How do I return a matrix for element in the first row, second column, instead of returning a vector?

A

m[1, 2, drop=FALSE]

Note that the drop argument can only be passed to the [] subsetting operation, and has no effect when passed to [[]] (which always returns a vector)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you extract the foo element of x by name using the $ syntax?

A

x$foo

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How do apply names to elements in a vector x with two elements?

A

names(x) <- c(“first”, “second”)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are two nice things about naming elements, and referencing elements by name?

A

Makes for cleaner code

Don’t have to remember where in the list/vector an element is

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What two things can you do with the [[]] syntax that you cannot do with the $ syntax?

A

Extract a sequence of elements

Allowing the use of computed indices

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

If you had a named element in x called “foo”, how would you perform partial matching to return the element “foo” with the [[]] syntax?

A

x[[“f”, exact = FALSE]]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does the R code for removing NA’s in a vector look like?

A

x <- is.na(x)

x[!bad]

17
Q

Describe the function of complete.cases

A

a logical vector with the value TRUE for rows that are complete, and FALSE for rows that have some NA values. To remove the rows with missing data from airquality, try the following:

> x <- airquality[complete.cases(airquality),]
> str(x)

18
Q

Given x <- list(2, “a”, “b”, TRUE)

A

x returns the entire list
x[2] returns a list which contains only the second element of the list
x[[2]] returns only the second element of the list: a character vector

19
Q

na.omit()

A

Returns a new dataset without any missing (NA) values

20
Q
A