Indexing Flashcards

1
Q

What are the six ways of indexing values in R?

A
  1. Positive Integers
  2. Negative Integers
  3. Zero
  4. Blank Space
  5. Names
  6. Logical Values
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are the three basic indexing operators? What is each one used for?

A
  • [] - used to return multiple items of an object. Returns an object with the same class as the original
  • [[] ] - used to extract one element of a list or data frame. The object that is returned is not necessarily a list or data frame.
  • $ - similiar to [[] ] , it’s used to extract a named element from a list or data frame.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How can I access values from indexes 1, 4 and 8 of the following vector?

cont = c(8, 4, NA, 9, 6, 1, 7, 9)

A

cont [c(1, 4, 8)]

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

What will this code return? Why?

cont = c(8, 4, NA, 9, 6, 1, 7, 9)
cont[-4]

A

8 4 NA 6 1 7 9

Because negative integer indexes return all values except for the one in the position specified after the negative sign, which in this case was 4.

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

What’s the return of this code?

cont = c(8, 4, NA, 9, 6, 1, 7, 9)
cont[-c(1, 4, 8)]

A

4 NA 6 1 7

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

What’s happening in this simple code?

cont[1:5]

A

We’re creating a sequence from 1 to 5 and using it to select elements of index 1 to 5 from object cont.

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

How can I select all elements that are not NA in vector v?

A

v[!is.na(v)]

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

How can I replace all NAs by 0 in my vector v?

A

v[is.na(v)] = 0

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

What is this code doing with my vector v?

is.na(v) = 3

A

It’s assigning NA to the 3rd element of vector v

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

What’s the difference between this two indexing codes on my vector v?

v[0]

v[]

A

v[0] - will return an empty vector (length 0). Rarely used.

v[] - will return all elements of the vector. Can be useful for matrices.

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

How can I easily assign letters (a,b,c …) as the names of my vector v elements?

A

names(v) = letters[1:length(v)]

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

How can I access that value that is in the third column and in the second row of my matrix mat?

A

mat[2, 3]

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

How can I access all values of the second column of my matrix mat? Why is that code valid?

A

mat[, 2]

This code is valid because blank space is also a valid way of indexing, meaning all values.

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

What is this code trying to attempt? What is a requirement for it to work?

mat[1, “C”]

A

It is trying to select the element of the first row and column C of matrix mat. The matrix must have named columns for it to work.

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

What’s are the outputs of this code? Why?

lis = list(c(3, 8, 7, 4), mat, 5:0)
class( lis[1] )
class( lis[[ 1] ] )

A
  • *class( lis[1] )** returns “list”
  • *class( lis[[ 1] ] )** returns “numeric”

This is because when I use operator [] I’m getting a subset of the original object, which is of the same class as the object, in this case list.

When I use operator [[] ] I return one element with it’s own class, which in this case is a numeric vector.

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

I have one list called lis with a matrix in this list. I’m using this code to get the matrix. What is the main requirement for this code to work?

lis$mat

A

The matrix inside the list should be a named element called ‘mat’.
$ operator selects elements based on their names.

17
Q

I have a list called lis with a matrix named mat in it, among other elements. Why doesn’t this code work? How can I fix it?

lis[‘mat’][2,2]

A

It doesn’t work because the operator [] returns a list with the matrix as the only element. Then I try to apply a two dimensional selector [2,2] on this list, and it doesn’t work because the list is one dimensional.

To fix that I can either use lis[[ ‘mat’] ] [2,2] or lis$mat[2,2] since both operators will return the matrix itself instead of a list.

18
Q

Will this code work? Why? What will I get?

da = data.frame(A = 4:1, B = c(2, NA, 5, 8))

da[2, 1]

A

Yes, because since data frames have two dimensions I can use the same indexing style as in matrix.

It will return the element in the second row, first column of the data frame.

19
Q

If I have the following data frame, what’s the different between the two selections below?
da = data.frame(A = 4:1, B = c(2, NA, 5, 8))

da[, “B”]

da[, “B”, drop = FALSE]

A

The first one returns the vector that was defined for column B of the dataframe. This vector has no attributes like dimensions, class, names, etc..

The second one returns a dataframe with only column B. It kees attributes such as class, row.names and names.

20
Q

My data frame da has a column called “A”. How can I get the elements of this column. What’s behind this indexing logic?

A

da$A

Data frames are kinds of lists, so you can use operator $ to access names elements of this list, which we also call “columns” in the context of the data frame.

21
Q

I have a data frame da. What will this two lines return?
da = data.frame(A = 4:1, B = c(2, NA, 5, 8))

da[1]
class( da[1] )

A

  • *da[1]** - returns the first column of the data frame
  • *class( da[1] )** - returns data.frame because I’m keeping the class by using operator []
22
Q

I have a data frame da. What will this two lines return?
da = data.frame(A = 4:1, B = c(2, NA, 5, 8))

da[[1]]
class( da[[1]] )

A
  • *da[[1]]** - returns the first column of the data frame as a vector
  • class( da[[1]] )** - returns *integer because this is the type class of the vector generated by 4:1 and [[] ] operator returns the element in the list (dataframe) with it’s own class.
23
Q

What’s the way of selecting all records that don’t have NA for column “B” of my data frame da using is.na function?

A

da[!is.na(da$B) ,]

24
Q

What’s the way of selecting all records that don’t have NA for column “B” of my data frame da, without using is.na function?

A

da[complete.cases(da),]

25
Q

How can I calculate the mean of column A of my data frame da, using function with?

A

with(da, mean(A))

26
Q

Considering this vector vec, how can I select the values that are greater than 15 and smaller than 35?

vec = c(5, 15, 42, 28, 79, 4, 7, 14)

A

vec [vec > 15 & vec < 35]

27
Q

Considering this data frame called games, how can I select only the row for US and CA?

country player
1 US 11
2 US 12
3 BR 13
4 CA 14
5 AR 15

A

games[games$country %in% c(‘US’, ‘CA’),]

28
Q

Considering my vector vec, how can I get the positions of the elements that are greater than 20? Explain how it works

vec = c(5, 15, 42, 28, 79, 4, 7, 14)

A

which( vec > 20)

Function which returns the TRUE indices of a logical vector. In this case I’m applying it to the logical vector that is created from my logical operation “vec > 20”

29
Q

Considering this data frame called games, how can I select only the rows for US, getting only column ‘player’, using function subset?

country player
1 US 11
2 US 12
3 BR 13
4 CA 14
5 AR 15

A

subset(games, country == ‘US’, select=player)