Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop single variable data.frame becoming a vector?

Tags:

r

When subsetting a data.frame with asking for only one variable we get a vector. This is what we ask for, so it is not strange. However, in other situations (if we ask for more then one column), we get a data.frame object. Example:

> data <- data.frame(a=1:10, b=letters[1:10])
> str(data)
'data.frame':   10 obs. of  2 variables:
 $ a: int  1 2 3 4 5 6 7 8 9 10
 $ b: Factor w/ 10 levels "a","b","c","d",..: 1 2 3 4 5 6 7 8 9 10
> data <- data[, "b"]
> str(data)
 Factor w/ 10 levels "a","b","c","d",..: 1 2 3 4 5 6 7 8 9 10

If I need my data object not to change it's type from data.frame no matter if it has only one variable, what do I have to do? The only thing that comes to my mind is:

data <- data[, "a"]
data <- as.data.frame(data)

...but this seems terribly redundant. Is there a better way, i.e. a way of saying "stay a data.frame, just give me a certain column"?

The problem is that I need:

  • to subset using vectors of variable names of different length
  • get data.frames with names unchanged as an output each time.
like image 763
Tim Avatar asked Jan 10 '23 03:01

Tim


1 Answers

The best is to use list subsetting. All of these will return a data.frame:

data['a']

data[c('a')]

data[c('a', 'b')]

Using matrix subsetting, you would have to add drop = FALSE:

data[, 'a', drop = FALSE]
like image 93
flodel Avatar answered Jan 11 '23 18:01

flodel