Prevent automatic conversion of single column to vector

前端 未结 2 1119
悲&欢浪女
悲&欢浪女 2020-12-10 02:49

I have a data frame like this:
df = data.frame(a=1:3, b=2:4, c=3:5)

I am selecting columns from that data frame using something akin to:

相关标签:
2条回答
  • 2020-12-10 03:11

    Also without the comma, i.e. df[c(T,F,F)], returns a dataframe but it sounds like you have cases where the new dataframe can have one or more columns. So use the drop option abovee

    0 讨论(0)
  • 2020-12-10 03:28

    This one is pretty simple. Append , drop = FALSE to your subsetting.

    E.g.

    df[, c(T, F, F), drop = FALSE]

    Also works for matrices.

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