Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否可以使用R中的SET with data.table分配多个列/值?_R_Data.table_Set - Fatal编程技术网

是否可以使用R中的SET with data.table分配多个列/值?

是否可以使用R中的SET with data.table分配多个列/值?,r,data.table,set,R,Data.table,Set,是否可以使用“set”将值分配给多个列 这里有一个例子。对于上下文,我想创建两组新列——一组将缺失的/NA值插补为0,另一组指示是否插补了缺失的值。第一组列将与现有列重复,但以0代替NA,并带有后缀“\u M0”。第二组列将为0/100,并带有后缀“\u MISS” 我将使用iris数据帧作为起点 ## create a copy of the iris data frame that i can modify local_iris <- copy(iris) ## make the lo

是否可以使用“set”将值分配给多个列

这里有一个例子。对于上下文,我想创建两组新列——一组将缺失的/NA值插补为0,另一组指示是否插补了缺失的值。第一组列将与现有列重复,但以0代替NA,并带有后缀“\u M0”。第二组列将为0/100,并带有后缀“\u MISS”

我将使用iris数据帧作为起点

## create a copy of the iris data frame that i can modify
local_iris <- copy(iris)
## make the local iris copy a data.table
iris.dt <- setDT(local_iris)
我在这里只使用“Sepal”列,所以我想保存这些名称并基于它们创建新的列名

## 'grep' returns a list of the positions that meet the criteria; 'grepl' returns a logic vector of the same length as the argument
## using the result of grep as the index/columns of a list seems to do the trick, even if it seems a tiny bit repetitive/clunky
bert <- names(iris.dt)[grep("^Sepal", names(iris.dt))]

## create lists like the original list with new suffixes
bert_M0 <- paste0(bert, "_M0")
bert_MISS <- paste0(bert, "_MISS")

谢谢

我认为这解决了你的问题

(j在序列长度(bert))集合中的
(
作品,
其中(是.na(opus[[bert[j]]]),
c(bert_M0[j],bert_MISS[j])
列表(0,100)
)

基本上是以字符向量的形式提供列名,以列表的形式提供值,我认为这解决了您的问题

(j在序列长度(bert))集合中的
(
作品,
其中(是.na(opus[[bert[j]]]),
c(bert_M0[j],bert_MISS[j])
列表(0,100)
)

基本上,您提供列名作为字符向量,值作为列表

您是否检查了
set
函数的手册?你的问题应该很容易找到答案,我找到了
set
出现在
:=
下,但没有一个示例实际使用
set
。我没有看到任何像
set(DT,I,。(col1,col2),(val1,val2)这样的例子
,但文档并非详尽无遗。我填写了。令我惊讶的是,到目前为止还没有。您是否查阅了
set
函数手册?如果您有问题,您应该可以在那里轻松找到答案。我查阅了。
set
出现在
:=
下,但没有一个示例实际使用
set
。我没有看到任何示例像
set(DT,i,(col1,col2),(val1,val2)
这样的函数,但文档并不详尽。我填写了。令我惊讶的是,到目前为止还没有。是的,这很有效!我仍然在计算何时需要使用
c(var1,var2)
以及何时可以使用列表,如
(var1,var2)
。出于好奇,我检查了
是否可以用来代替值部分的
列表,答案是否定的。手册中解释的所有内容,请参见?c和?data.table是的,这很有效!我仍然在计算何时需要使用
c(var1,var2)
以及何时可以使用列表,如
(var1,var2)
。出于好奇,我检查了
是否可以代替值部分中的
列表
,答案是否。手册中解释的所有内容,请参见?c和?data.table
## 'grep' returns a list of the positions that meet the criteria; 'grepl' returns a logic vector of the same length as the argument
## using the result of grep as the index/columns of a list seems to do the trick, even if it seems a tiny bit repetitive/clunky
bert <- names(iris.dt)[grep("^Sepal", names(iris.dt))]

## create lists like the original list with new suffixes
bert_M0 <- paste0(bert, "_M0")
bert_MISS <- paste0(bert, "_MISS")
## the best way to go about this is unclear
## i will settle for 'a' way and worry about 'best' way later
## one approach is to extend the data.table to have the new columns added, and then modify their respective values in place later

## create a copy of the relevant columns
M0<-iris.dt[, .SD, .SDcols = bert]

## rename the columns
setnames(M0, old = bert, new = bert_M0)

## create a new data.table with the copied columns
opus<-cbind(iris.dt, M0)

## this creates a set of indicators and sets all the _MISS columns equal to 0
opus[, (bert_MISS) := 0L]
## try using "set"
for (j in seq_len(length(bert))) { # seq_len(arg) is an alternative way of writing 1:arg
   set(opus, ## the data.table we are operating on
       which(is.na(opus[[bert[j]]])), ## the values of i
       bert_M0[j], ## the column
       0 ## the value
       )
   set(opus, ## the data.table we are operating on
       which(is.na(opus[[bert[j]]])), ## the values of i
       bert_MISS[j], ## the column
       100 ## the value
   )
}