r - Slicing an array with same return type -


depending on number of selected column, return type of dataframe slicing changes, 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 correct r syntax 10*k dataframe ?

you have been bitten infamous drop 'feature' in r. please use str(df[ , colselect, drop=false]).

for 'interactive' convenience, r tends drop other dimensions of array if choose single element of dimension. therefore 10 x 1 data.frame becomes vector of length 10.

for more fun on topic (and other things), please read section 8.1.44 of r-inferno. also, on r interpreter, try ?'['.


Comments