假设我有一个数据表,我想根据旧变量的几个条件计算一个新变量,如下所示:
library(data.table) test <- data.table(a = c(1,1,0), b = c(0,1,0), c = c(1,1,1)) test[a==1 & b==1 & c==1,test2:=1]但实际上我有更多的条件(不同变量的所有组合),它们也有不同的长度。 我从列表中绘制这些:
conditions<-list(c("a","b","c"), c("b","c"))然后我想循环遍历该列表并构建一个像这样的字符向量(在删除它并转到列表的下一个元素之前我想做一些事情):
mystring <- paste0(paste0(conditions[[1]], collapse = "==1 & "), "==1")但是如何在data.table使用“mystring”? as.function()或get()或eval()似乎不起作用。 就像是:
test[mystring,test3:=1]是我正在寻找的。
Say I have a data table and I want to calculate a new variable based on several conditions of the old variables like this:
library(data.table) test <- data.table(a = c(1,1,0), b = c(0,1,0), c = c(1,1,1)) test[a==1 & b==1 & c==1,test2:=1]But I actually have many more conditions (all combinations of the different variables) which also have a different length. I draw those from a list such as:
conditions<-list(c("a","b","c"), c("b","c"))and then I want to loop through that list and build a character vector like this (with which I want to do something before deleting it and going to the next element of the list):
mystring <- paste0(paste0(conditions[[1]], collapse = "==1 & "), "==1")But how can I use "mystring" inside the data.table? as.function() or get() or eval() don't seem to work. Something like:
test[mystring,test3:=1]is what I'm looking for.
最满意答案
对于给定的用例,您可以使用on = with on =来实现所需的目标,而无需创建和评估复杂的条件字符串。
代替
test[a==1 & b==1 & c==1, test2 := 1][]我们可以写
test[.(1, 1, 1), on = c("a", "b", "c"), test2 := 1][] # a b c test2 #1: 1 0 1 NA #2: 1 1 1 1 #3: 0 0 1 NA现在,OP已经要求使用lapply() “做某事”循环一系列条件。 这可以如下实现
# create list of conditions for subsetting col = list(c("a","b","c"), c("b","c")) val = list(c(1, 1, 1), c(0, 1)) # loop over conditions lapply(seq_along(col), function(i) test[as.list(val[[i]]), on = col[[i]], test2 := i]) #[[1]] # #[[2]] # a b c test2 #1: 1 0 1 2 #2: 1 1 1 1 #3: 0 0 1 2请注意,未使用lapply()的输出,因为test已在适当的位置进行了修改:
test # a b c test2 #1: 1 0 1 2 #2: 1 1 1 1 #3: 0 0 1 2For the given use case, you may use join with on = to achieve the desired goal without having to create and evaluate complex strings of conditions.
Instead of
test[a==1 & b==1 & c==1, test2 := 1][]we can write
test[.(1, 1, 1), on = c("a", "b", "c"), test2 := 1][] # a b c test2 #1: 1 0 1 NA #2: 1 1 1 1 #3: 0 0 1 NANow, the OP had requested to loop over a list of conditions using lapply() "to do something". This can be achieved as follows
# create list of conditions for subsetting col = list(c("a","b","c"), c("b","c")) val = list(c(1, 1, 1), c(0, 1)) # loop over conditions lapply(seq_along(col), function(i) test[as.list(val[[i]]), on = col[[i]], test2 := i]) #[[1]] # #[[2]] # a b c test2 #1: 1 0 1 2 #2: 1 1 1 1 #3: 0 0 1 2Note that the output of lapply() is not used because test has been modified in place:
test # a b c test2 #1: 1 0 1 2 #2: 1 1 1 1 #3: 0 0 1 2如何在data.table的“i”部分中使用粘贴的复杂结果?(How to use a complicated result from paste inside data.table's “i” part?)假设我有一个数据表,我想根据旧变量的几个条件计算一个新变量,如下所示:
library(data.table) test <- data.table(a = c(1,1,0), b = c(0,1,0), c = c(1,1,1)) test[a==1 & b==1 & c==1,test2:=1]但实际上我有更多的条件(不同变量的所有组合),它们也有不同的长度。 我从列表中绘制这些:
conditions<-list(c("a","b","c"), c("b","c"))然后我想循环遍历该列表并构建一个像这样的字符向量(在删除它并转到列表的下一个元素之前我想做一些事情):
mystring <- paste0(paste0(conditions[[1]], collapse = "==1 & "), "==1")但是如何在data.table使用“mystring”? as.function()或get()或eval()似乎不起作用。 就像是:
test[mystring,test3:=1]是我正在寻找的。
Say I have a data table and I want to calculate a new variable based on several conditions of the old variables like this:
library(data.table) test <- data.table(a = c(1,1,0), b = c(0,1,0), c = c(1,1,1)) test[a==1 & b==1 & c==1,test2:=1]But I actually have many more conditions (all combinations of the different variables) which also have a different length. I draw those from a list such as:
conditions<-list(c("a","b","c"), c("b","c"))and then I want to loop through that list and build a character vector like this (with which I want to do something before deleting it and going to the next element of the list):
mystring <- paste0(paste0(conditions[[1]], collapse = "==1 & "), "==1")But how can I use "mystring" inside the data.table? as.function() or get() or eval() don't seem to work. Something like:
test[mystring,test3:=1]is what I'm looking for.
最满意答案
对于给定的用例,您可以使用on = with on =来实现所需的目标,而无需创建和评估复杂的条件字符串。
代替
test[a==1 & b==1 & c==1, test2 := 1][]我们可以写
test[.(1, 1, 1), on = c("a", "b", "c"), test2 := 1][] # a b c test2 #1: 1 0 1 NA #2: 1 1 1 1 #3: 0 0 1 NA现在,OP已经要求使用lapply() “做某事”循环一系列条件。 这可以如下实现
# create list of conditions for subsetting col = list(c("a","b","c"), c("b","c")) val = list(c(1, 1, 1), c(0, 1)) # loop over conditions lapply(seq_along(col), function(i) test[as.list(val[[i]]), on = col[[i]], test2 := i]) #[[1]] # #[[2]] # a b c test2 #1: 1 0 1 2 #2: 1 1 1 1 #3: 0 0 1 2请注意,未使用lapply()的输出,因为test已在适当的位置进行了修改:
test # a b c test2 #1: 1 0 1 2 #2: 1 1 1 1 #3: 0 0 1 2For the given use case, you may use join with on = to achieve the desired goal without having to create and evaluate complex strings of conditions.
Instead of
test[a==1 & b==1 & c==1, test2 := 1][]we can write
test[.(1, 1, 1), on = c("a", "b", "c"), test2 := 1][] # a b c test2 #1: 1 0 1 NA #2: 1 1 1 1 #3: 0 0 1 NANow, the OP had requested to loop over a list of conditions using lapply() "to do something". This can be achieved as follows
# create list of conditions for subsetting col = list(c("a","b","c"), c("b","c")) val = list(c(1, 1, 1), c(0, 1)) # loop over conditions lapply(seq_along(col), function(i) test[as.list(val[[i]]), on = col[[i]], test2 := i]) #[[1]] # #[[2]] # a b c test2 #1: 1 0 1 2 #2: 1 1 1 1 #3: 0 0 1 2Note that the output of lapply() is not used because test has been modified in place:
test # a b c test2 #1: 1 0 1 2 #2: 1 1 1 1 #3: 0 0 1 2
发布评论