[]
is your selection operator for data objects [vectors, matrices, data frames]. As you might have realized, comma gives you the sense of ordering within data objects, however, in selection operator, comma is used for direction. To select an element of a vector, you would put the selection operator next to your vector. For example, myvector[2]
will give me the second element of the vector myvector
. To select something out of a 2D data object (matrices, data frames), simply use the syntax 2Dobject[row,column]
. The first index represents the row you want, and the second index represents the column you want.
Now you might be thinking, “What if I want multiple columns of one row?” This is where vector comes in. Recall that comma in vectors represents ordering, so say you want the second and forth column for the 5th row from 2Dobject
you would write 2Dobject[5,c(2,4)]
. Remember, you can also use :
or seq
or rep
or a combination of all these things as indices in your selection operator.