nicolas
nicolas

Reputation: 9815

Slicing an array with same return type

Depending on the number of selected column, the return type of dataframe slicing changes, as illsutrated below

> dim(df)
[1] 10  5
> colselect
[1]  TRUE FALSE FALSE FALSE FALSE
> colselect2
[1]  TRUE FALSE FALSE  TRUE FALSE
> str(df[,colselect])
 logi [1:10] TRUE TRUE TRUE TRUE FALSE FALSE ...
> str(df[,colselect2])
'data.frame':   10 obs. of  2 variables:
 $ a: logi  TRUE TRUE TRUE TRUE FALSE FALSE ...
 $ b: logi  FALSE TRUE TRUE FALSE TRUE TRUE ...

what would be the correct R syntax to always get back a 10*k dataframe ?

Upvotes: 0

Views: 39

Answers (1)

asb
asb

Reputation: 4432

You have been bitten by the infamous drop 'feature' in R. Please use str(df[ , colselect, drop=FALSE]).

For 'interactive' convenience, R tends to drop the other dimensions of an array if all you choose is a single element of a dimension. Therefore a 10 x 1 data.frame becomes a vector of length 10.

For some more fun on the topic (and other things), please read section 8.1.44 of the R-inferno. Also, on your R interpreter, try ?'['.

Upvotes: 3

Related Questions