【发布时间】:2018-05-11 18:40:42
【问题描述】:
我有一个数据框
df = data.frame(V1 = 1, V2 = 2, V3 = 3, V4 = 4, V5 = 5, respondent_ID = "ID1", respondent_attribute = "Attribute1")
并希望将其拆分为两个数据帧,其中一个将包含列 V1、V2、V3、respondent_ID 和 respondent_attribute,另一个将包含 V4、V5、respondent_ID 和 respondent_attribute。
我可以相对轻松地做到这一点
newdf <- subset(df, select = c(1:3, 6:7))
然后从 df 中手动删除列 V1、V2 和 V3
df[1:3] = NULL
但我怀疑有一个更优雅(也许是更好的做法)的解决方案。有什么建议吗?
【问题讨论】:
-
您可以只使用
df[,c(1,2,3,6,7)]或df[,c("V1","V2","V3","respondent_ID","respondent_attribute")]形式(第二个类似),具体取决于您是否知道列索引