简体   繁体   English

R,data.table:如何根据存储在字符向量中的其他列的名称来分配某些列的值?

[英]R, data.table: How to assign values of some columns based on the names of other columns, which are stored in a character vector?

I have the following data table: 我有以下数据表:

dt=data.table(a=seq(1,3),b=letters[seq(1,3)],
          c=seq(4,6),d=letters[seq(4,6)])
dt
   a b c d
1: 1 a 4 d
2: 2 b 5 e
3: 3 c 6 f

I store the names of some columns in one vector and the names of other columns in another vector: 我将一些列的名称存储在一个向量中,将其他列的名称存储在另一个向量中:

names1=names(dt)[1:2]
names2=names(dt)[3:4]

I need to assign the values of the columns stored in names2 to the columns of names1 when some conditions apply. 当某些条件适用时,我需要将names2中存储的列的值分配给names1的列。 Something like 就像是

dt[c(2,3),names1:=names2]

dt
   a b c d
1: 1 a 4 d
2: 5 e 5 e
3: 6 f 6 f

I have tried the following syntax without success: 我尝试了以下语法但没有成功:

dt[c(2,3),names1:=dt[c(2,3),names2,with=F]]

But it still tries to assign the values of the string vectors contained in names2 但它仍然试图分配names2中包含的字符串向量的值

I would do 我会做

dt[2:3, (names1) := .SD, .SDcols = names2]

Or an alternative approach, thanks to @DavidArenburg: 或者另一种方法,感谢@DavidArenburg:

dt[c(2,3), (names1) := mget(names2)]

How it works 这个怎么运作

  • As @DavidArenburg explained, the parentheses in (names1) := ensure that we look at the content of the names1 vector instead of making a column named "names1" . 正如@DavidArenburg解释的那样, (names1) := names1)中的括号(names1) :=确保我们查看names1向量的内容,而不是创建名为"names1"的列。
  • I'd suggest going through the first vignette for the package for details on .SDcols . 我建议通过第一 .SDcols 插图获取有关.SDcols详细信息。
  • mget finds objects based on their names and puts them into a list. mget根据名称查找对象并将其放入列表中。

I found that I can do this using with=F, is it inefficient in some way?. 我发现我可以用= F来做这个,它在某种程度上效率低吗?

The way I found it was like this: 我发现它的方式是这样的:

dt[c(2,3),names1:=dt[c(2,3),names2,with=F],with=F]

Any comments on the efficiency would be nicely received. 任何关于效率的评论都会很好地收到。

Thank you. 谢谢。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 R数据表的求和列,其名称存储在一个向量中 - Sum columns of R data table, the names of which are stored in a vector 具有data.table变量的函数,其名称存储在字符向量中 - functions with data.table variables which names are stored in a character vector 使用列名的字符向量访问data.table中的列 - Accessing columns in data.table using a character vector of column names R,data.table:对名称存储在向量中的所有列求和 - R, data.table: Sum all columns whose names are stored in a vector R data.table - 将字符向量中的列除以另一个字符向量中的列 - R data.table - dividing columns in character vector by columns in another character vector data.table:根据指标列值和名称创建新字符列 - data.table: Create new character column based on indicator columns values and names R - 如何根据包含日期序列的向量的值将列添加到数据框/data.table - R - How can I add columns to a data frame / data.table based on values of a vector that contains a sequence of dates R data.table:根据其他列的值标记连续值的“标识”? - R data.table: label “identity” of consecutive values based on values of other columns? 当变量名称存储在字符向量中时选择/分配给 data.table - Select / assign to data.table when variable names are stored in a character vector R: Add boolean column to a data.table based on return values of a function which evaluates two columns from different data.table - R: Add boolean column to a data.table based on return values of a function which evaluates two columns from different data.table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM