Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.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 当列名基于另一列的值时,获取列的值_R_Get_Data.table - Fatal编程技术网

R 当列名基于另一列的值时,获取列的值

R 当列名基于另一列的值时,获取列的值,r,get,data.table,R,Get,Data.table,我有一个包含一百万条记录的数据表,我尝试基于月创建一个新列。idx: dt[, new_col := get(paset0("month_",month.idx)] 它只适用于第一行 有人能帮我解决这个问题吗?谢谢 Data id month_1 month_2 month_3 month_4 month_5 month.idx 1: x1 1 1 1 0 1 3 2: x2 0 0

我有一个包含一百万条记录的数据表,我尝试基于
月创建一个新列。idx

dt[, new_col := get(paset0("month_",month.idx)]
它只适用于第一行

有人能帮我解决这个问题吗?谢谢

Data
    id month_1 month_2 month_3 month_4 month_5 month.idx
1:  x1       1       1       1       0       1         3
2:  x2       0       0       0       1       0         4
3:  x3       1       0       0       0       0         1
4:  x4       0       0       0       0       0         5
5:  x5       1       1       0       0       1         2
6:  x6       0       1       0       1       1         3
7:  x7       0       0       1       1       1         4
8:  x8       0       0       0       0       0         1
9:  x9       0       0       0       0       1         5

results:
    id month_1 month_2 month_3 month_4 month_5 month.idx new_col
1:  x1       1       1       1       0       1         3       1
2:  x2       0       0       0       1       0         4       0
3:  x3       1       0       0       0       0         1       0
4:  x4       0       0       0       0       0         5       0
5:  x5       1       1       0       0       1         2       0
6:  x6       0       1       0       1       1         3       0
7:  x7       0       0       1       1       1         4       1
8:  x8       0       0       0       0       0         1       0
9:  x9       0       0       0       0       1         5       0

expected:
    id month_1 month_2 month_3 month_4 month_5 month.idx new_col
1:  x1       1       1       1       0       1         3       1
2:  x2       0       0       0       1       0         4       1
3:  x3       1       0       0       0       0         1       1
4:  x4       0       0       0       0       0         5       0
5:  x5       1       1       0       0       1         2       1
6:  x6       0       1       0       1       1         3       0
7:  x7       0       0       1       1       1         4       0
8:  x8       0       0       0       0       0         1       0
9:  x9       0       0       0       0       1         5       1
这里有两个选项:

1)使用
get
逐行接收Frank的评论:

DT[, new_col := get(paste0("month_", month.idx)), by= month.idx]
2)熔化,然后加入以进行查找

DT[, variable := paste0("month_", month.idx)]
DT[melt(DT, id.vars="id", measure.vars=patterns("^month_")), 
    on=.(id, variable), new_col := value]
速度取决于您拥有的行数和月列数

数据:

DT这里有两个选项:

1)使用
get
逐行接收Frank的评论:

DT[, new_col := get(paste0("month_", month.idx)), by= month.idx]
2)熔化,然后加入以进行查找

DT[, variable := paste0("month_", month.idx)]
DT[melt(DT, id.vars="id", measure.vars=patterns("^month_")), 
    on=.(id, variable), new_col := value]
速度取决于您拥有的行数和月列数

数据:


DT提供和测试再现问题的代码通常是有帮助的。(在本例中,我们没有创建data.table的代码,您也没有测试并注意到打字错误paset0)如果您感兴趣,例如对于下一个问题,这里有一些指导:提供和测试再现问题的代码通常是有帮助的。(在本例中,我们没有创建data.table的代码,您也没有测试并注意到拼写错误paset0)如果您感兴趣,例如对于下一个问题,这里有一些指导:重新加速,如果您按月分组,第一种方法可能会更快。idx而不是行(如果行比列多很多)重新加速,如果按month.idx分组而不是按行分组(如果行比列多得多),第一种方法可能会更快