1

I'm in the process of learning R and hoped for some clarification on something.

Given this dataframe:

myDataset <- data.frame("IDs" = rep(1:10,each = 5),
                        "session" = rep(1:5, times = 10), 
                        "IV" = rnorm(50),
                        "DV" = rnorm(50))

What different uses would calling the first column from this dataframe have based on whether you used one set of brackets:

myDataset[1]

Or two sets of brackets:

myDataset[[1]]

They both give the same information, one in the form of the numbers listed in rows and the other in its original column form.

I'm just trying to understand why I might want to use one over the other.

1 Answers1

0

A data.frame is a list with columns of equal length. By using [[, we extract the column as a vector, while with [, get a data.frame with single or multiple columns. Another option to return a vector with [ is to specify the , to indicate explicitly that it is a column index and by default then the drop = TRUE gets triggered for data.frame

myDataset[, 1]

If we still want a data.frame single column

myDataset[, 1, drop = FALSE]

  
akrun
  • 874,273
  • 37
  • 540
  • 662