Slicing a Dataframe in R

Mar 10, 2015 • Jon Borrelli

I made these questions based on the Analyzing Patient Data lesson from the R programming lessons.

Question 1

Suppose you had a dataframe named df:

A B C D E
02 66 04 94 12
78 97 05 44 71
92 32 55 17 73
18 82 96 01 38
49 25 15 80 02

How could I ask R to return the 3rd and 5th rows of the dataframe?

  1. df[3,5]
  2. df[c(3,5)]
  3. df(c(3,5),)
  4. df[c(3,5),]

Question 2

With the same dataframe as above, what would the following code produce?

max(df[2:4, c(1,4)])
  1. 92
  2. 96
  3. 78
  4. 97

Misconceptions

  1. Correct answer
  2. They have switched rows and columns
  3. Not understood that 2:4 means rows 2, 3, and 4 (not just 2 and 4)
  4. Not understood that c(1,4 is different from 1:4