根据data.table中一列的值更改多列的值 [英] Change values of multiple columns based on the value of one column in data.table

查看:70
本文介绍了根据data.table中一列的值更改多列的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个数据表, dt1

Suppose I have a data table, dt1:

dt1 <- data.table(
names = c("A1", "XX", "A2", "XY", "A3", "XZ"),
   A1 = c( 0,    0,    0,    0,    0,    0), 
   A2 = c( 0,    0,    0,    0,    0,    0), 
   A3 = c( 0,    0,    0,    0,    0,    0)
)

我想要新的数据表,例如:

I want the new data table like:

dt2 <- data.table(
names = c("A1", "XX", "A2", "XY", "A3", "XZ"),
   A1 = c( 1,    0,    0,    0,    0,    0), 
   A2 = c( 0,    0,    1,    0,    0,    0), 
   A3 = c( 0,    0,    0,    0,    1,    0)
)

即,如果列 names 的行值与某些列的名称相同,则该列的行值将更改为 1

i.e, if the row value of the column names is the same as the names of certain column, then the row value of that column is changed to 1.

我可以通过以下代码实现:

I can achieve this via the following code:

dt1[names == "A1", "A1" := 1]
dt1[names == "A2", "A2" := 1]
dt1[names == "A3", "A3" := 1]

但我想知道是否存在更简单的方法,尤其是当我要更改的列数很大时。

But I'm wondering whether there is an easier way to do this, especially when the number of columns I want to change is big.

我尝试了以下几行,但它们不起作用:

I've tried the following lines, and they are not worked:

cln <- c("A1", "A2", "A3")
dt1[names == (cln), (cln) := 1]


推荐答案

使用有效的 for(...)set(... ) 数据.table

for(j in names(dt1)[-1]) {
  set(dt1, dt1[, .I[names == j]], j, value = 1)
}

给出:


> dt1
   names A1 A2 A3
1:    A1  1  0  0
2:    XX  0  0  0
3:    A2  0  1  0
4:    XY  0  0  0
5:    A3  0  0  1
6:    XZ  0  0  0


也可以使用 setdiff(names(dt1), names)代替 names(dt1)[-1]

Instead of names(dt1)[-1] you can also use setdiff(names(dt1), "names").

这篇关于根据data.table中一列的值更改多列的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆