Search code examples
rdataframer-factor

Selecting one column of a data frame returns a factor, instead of another data frame


I have the below code, if m ==2 then cd remains a dataframe and names(cd) are from the original dataframe d. However if m == 3 only one column in the dataframe remains and cd turns into a factor and I lose the names...

samplesize <-100
g1 <- gl(2,samplesize/2,labels=c("V","M"))
g2 <- gl(3,samplesize/3,labels=c("V","M","U"))
m <- 2
d <- data.frame(g1,g2)
l <-  sapply(d,nlevels)
cd <- d[,l <= m]
names(cd)

I would like to keep the names of d even if the filter only leaves one column?


Solution

  • Use drop=FALSE to avoid coercing to the lowest dimension.

    cd <- d[,l <= m, drop=FALSE]
    names(cd)
    [1] "g1"