简体   繁体   中英

R create dynamically subset of data.table

I would like to create dynamically subsets of my data.table based on the values in some columns.

In my data.table, I have the following variables: owner,2G,3G,4G. 2G,3G,4G are binary.

I want to create three subsets: one where 2G==1, one where 3G==1, one where 4G==1.

Example:

a=c("Paul",1,1,0)
b=c("George",1,0,0)
x=cbind(a,b)
colnames(x)=c("Owner","2G","3G","4G")

Here is my code:

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) 
}

My new data.tables are created, but empty. I have tried several things (with eval, quote, change the syntax etc...) but I fail to call properly the column.

EDIT:

I have tried something else, but it also fails:

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.

Why does it says "'column name 'index' is not found"? Why isn't it the value of "index" that is taken in account? Eval index doesn't change anything.

Vectors and matrixes cannot contain both numbers and characters, in your case numbers are converted to characters.

This will work better to define your table, but column names can't start with a number in a data.frame

x <- data.frame(Owner = c("Paul","George"),
                G2 = c(1,1),
                G3 = c(1,0),
                G4 = c(0,0),
                stringsAsFactors= FALSE)

then here's your subset

subset(x,G3 == 1)

(also, you've used cbind instead of rbind in your question, you may want to edit it)

I finally found the answer:

for(value in techno){
  set1=subset(arcep,arcep[,get(colnames(arcep)[grep(value,colnames(arcep))])]==1)
  assign(paste0("ARCEP_",value),set1)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM