How can I stop a single data.frame variable from becoming a vector?

With a subset of a data.frame

with a request for only one variable, we get a vector. This is what we are asking for, so it is not strange. However, in other situations (if we are requesting more than 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 object data

to not change its type from data.frame

regardless of whether it has only one variable, what should I do? The only thing that comes to my mind is:

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

      

... but that seems terribly redundant. Is there a better way, that is, a way of saying, "Stay data.frame

, just give me a certain column"?

The problem is that I need:

  • for a subset using vectors of variable names of varying length
  • get data.frames

    named unchanged as output each time.
+3


source to share


1 answer


It is best to use a subset of lists. They will all return data.frame:

data['a']

data[c('a')]

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

      



Using a subset of matrices you need to add drop = FALSE

:

data[, 'a', drop = FALSE]

      

+7


source







All Articles