Data.frame becomes factor/vector after filtering/subsetting

前端 未结 1 1495
花落未央
花落未央 2021-01-12 07:38

I have a data.frame with one column, like so:

>d = data.frame(animal=c(\"horse\",\"dog\",\"cat\"))

then I filter it by excluding all ite

1条回答
  •  礼貌的吻别
    2021-01-12 08:09

    Welcome to R. You've just been bitten by the drop annoyance: you need to explicitly tell R not to "drop to one-dimension":

    res = d[!(d$animal %in% c("horse")), , drop = FALSE] 
    

    0 讨论(0)
提交回复
热议问题