r data.table通过引用其他列中的值添加新列和值
我有一个示例 data.table
如下:
> dt = data.table("Label" = rep(LETTERS[1:3], 3),
+ "Col_A" = c(2,3,5,0,2,7,6,8,9),
+ "Col_B" = c(1,4,3,5,2,0,7,5,8),
+ "Col_C" = c(2,0,4,1,5,6,7,3,0))
> dt[order(Label)]
Label Col_A Col_B Col_C
1: A 2 1 2
2: A 0 5 1
3: A 6 7 7
4: B 3 4 0
5: B 2 2 5
6: B 8 5 3
7: C 5 3 4
8: C 7 0 6
9: C 9 8 0
我想创建一个新列,它根据 Label 列从现有列中获取值。我想要的示例输出如下:
Label Col_A Col_B Col_C Newcol
1: A 2 1 2 2
2: A 0 5 1 0
3: A 6 7 7 6
4: B 3 4 0 4
5: B 2 2 5 2
6: B 8 5 3 5
7: C 5 3 4 4
8: C 7 0 6 6
9: C 9 8 0 0
逻辑是 Newcol
值引用基于 Label
列的相应列。例如,Label
列的前 3 行是 A
,因此 Newcol
列的前 3 行指的是前 3 行Col_A
列的。
我尝试使用代码 dt[, `:=` ("Newcol" = eval(as.symbol(paste0("Col_", dt$Label))))]
但它没有给出所需的输出。
I have a sample data.table
as below:
> dt = data.table("Label" = rep(LETTERS[1:3], 3),
+ "Col_A" = c(2,3,5,0,2,7,6,8,9),
+ "Col_B" = c(1,4,3,5,2,0,7,5,8),
+ "Col_C" = c(2,0,4,1,5,6,7,3,0))
> dt[order(Label)]
Label Col_A Col_B Col_C
1: A 2 1 2
2: A 0 5 1
3: A 6 7 7
4: B 3 4 0
5: B 2 2 5
6: B 8 5 3
7: C 5 3 4
8: C 7 0 6
9: C 9 8 0
I want to create a new column which takes values from the existing columns based on the Label column. My desired sample output is as below:
Label Col_A Col_B Col_C Newcol
1: A 2 1 2 2
2: A 0 5 1 0
3: A 6 7 7 6
4: B 3 4 0 4
5: B 2 2 5 2
6: B 8 5 3 5
7: C 5 3 4 4
8: C 7 0 6 6
9: C 9 8 0 0
The logic is that the Newcol
value refers to the respective columns based on the Label
column. For example, the first 3 rows of the Label
column is A
, so the first 3 rows of the Newcol
column refers to the first 3 rows of the Col_A
column.
I have tried using the code dt[, `:=` ("Newcol" = eval(as.symbol(paste0("Col_", dt$Label))))]
but it doesn't give the desired output.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用
fcase
:With
fcase
:我们可以使用
kit
包的矢量化 switch 函数,它像data.table
一样是fastverse
的一部分。We can use a vectorized switch function of the
kit
package, which likedata.table
is part of thefastverse
.如果您能够使用 dplyr 库,我会使用那里的 case_when 函数。
我没有测试过该代码,但它会是类似的东西。
If you are able to use the dplyr library, I would use the case_when function from there.
I've not tested that code but it would be something like that.