我想根据某些列中的值动态创建data.table的子集。R创建data.table的动态子集
在我的data.table中,我有以下变量:owner,2G,3G,4G。 2G,3G,4G是二进制的。
我想创建三个子集:一个是2G == 1,一个是3G == 1,一个是4G == 1。
例子:
a=c("Paul",1,1,0)
b=c("George",1,0,0)
x=cbind(a,b)
colnames(x)=c("Owner","2G","3G","4G")
这里是我的代码:
all_names_df=c()
for(value in 2:4){
techno=paste0(value,"G")
name=paste0("arcep",techno)
all_df=c(all_names_df,name)
df=arcep[techno==1]
assign(name,df)
}
创建我的新data.tables,而空。我已经尝试了几件事情(与评估,引用,更改语法等),但我没有正确调用列。
编辑:
我曾尝试别的东西,但它也失败:
techno=c("2G","3G","4G")
for(value in techno){
index=grep(value,colnames(arcep))
print(index)
set1=subset(arcep,arcep[,index]==1)
print(dim(set1))
assign(set1,paste0("ARCEP_",value))}
Error in `[.data.table`(arcep, , index) :
j (the 2nd argument inside [...]) is a single symbol but column name 'index' is not found. Perhaps you intended DT[,..index] or DT[,index,with=FALSE]. This difference to data.frame is deliberate and explained in FAQ 1.1.
为什么它说:““列名‘索引’未找到”?为什么它不是考虑到“索引”的价值?评估指标不会改变任何东西。
我试过别的: – Oolis
你正在使用'data.table',所以'arcep [,index]'等同于''arcep [[“index”]]''。另外,你想用'assign'做什么? –