16.3 Getting to know your personalized dataframe

Let’s get to know your personalized subset of the data.

16.3.1 Dataframe dimension

The following commands tell you the row x column dimension, number of rows, and number of columns.

dim(my.frogs)  #[_]
nrow(my.frogs) #[_]
ncol(my.frogs) #[_]

16.3.2 Optional: Accessing elements of objects

The following section is optional.

The commands dim(), nrow and ncol all generate objects that display information on the dimension of a dataframe. dim() produces and object that is a “vector” that is 1 row x 2 elements in size. We can select these individual elements using square brackets “[…]”

The full dim() output

dim(my.frogs)

The 1st element of the dim() output is accessed by appending “[1]” to the very end of the line of code

dim(my.frogs)[1] #"[1]" goes outside the ")"

The 2nd element

dim(my.frogs)[2]

Both elements: use “[1:2]” with a “:”.

dim(my.frogs)[1:2]

Another way to get both elements, using “c(…)”

dim(my.frogs)[c(1,2)]

What happens when you execute the command below command? Why?

dim(my.frogs)[c(2,1)]

If you want to know one reason why R can drive you crazy, run this code, without the “c” before “(1,2)”

dim(my.frogs)[(1,2)]

But then try this; same as above but with “:” instead of “,” between the numbers.

dim(my.frogs)[(1:2)]

Yes, R is that picky.

End optional section


16.3.3 Dataframe preview

The head() and tail() commands give us short previews of the dataframe.

head() gives the top few rows

head(my.frogs) #Note: I've truncated the output that I've actually shown

The bottom few rows with tail()

tail(my.frogs)

names() just gives of the names of the columns. It is the same as colnames().

names(my.frogs)

Again, if you want to know what the column names are, use the ? command

?my.frogs