Key elements of the visualization process
Environment - the R Language
18. Extracting specific rows and columns


Now you can create a data frame. Sometimes data frames can have hundreds of rows or columns. What if you want to work with a specific set of rows or columns? You have to extract those rows and columns from the data frame. To do that, you use one vector containing the position of the specific rows and another with the position of specific columns. For example:

family[c(1,2), c(2,4)]

Notice that the data frame name comes first, then the vector commands are in brackets. The desired row and column positions are in parenthesis.

This command will select rows 1 and 2 (the first vector is for rows) and columns 2 and 4 (the second vector is for columns). This will return:

   sex      marital_status
1   M           TRUE
2   M          FALSE

If you want to return all columns for specific rows, you simply omit the second vector (but remember the final comma!):

family[c(1,2), ]

Similarly, when you want specific columns for all rows, you omit the first vector (but keep the first comma!):

family[, c(2,4)]


Extract the first and third row and the first and second columns from zooDataset. When you're done, press the Run and Check Code button.

Stuck? Here's a hint!

You should write:

zooDataset[c(1,3), c(1,2)]