Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/79.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 lapply更新数据表列表。具有列表的表-级别1没有此类索引_R_List_Data.table_Lapply - Fatal编程技术网

R lapply更新数据表列表。具有列表的表-级别1没有此类索引

R lapply更新数据表列表。具有列表的表-级别1没有此类索引,r,list,data.table,lapply,R,List,Data.table,Lapply,我正在尝试使用列表更新date.tables列表,这似乎应该与本例中的情况相同: set.seed(1965) dt_lst <- list(dt1 <- data.table(a = rnorm(1:4), b = c(4,3,2,1)), dt2 <- data.table(c = rnorm(1:5), d = letters[1:5])) > dt_lst [[1]] a b 1: 0.8428429 4 2: 0.2958355 3 3:

我正在尝试使用列表更新date.tables列表,这似乎应该与本例中的情况相同:

set.seed(1965)
dt_lst <- list(dt1 <- data.table(a = rnorm(1:4),
b = c(4,3,2,1)), dt2 <- data.table(c = rnorm(1:5),
d = letters[1:5]))

> dt_lst
[[1]]
        a b
1:  0.8428429 4
2:  0.2958355 3
3: -1.0520980 2
4:  0.9628192 1

[[2]]
         c d
1: -0.05033855 a
2: -0.94065157 b
3:  1.20459624 c
4: -0.47791557 d
5: -0.30362496 e
set.seed(1965年)

dt_lst该问题似乎是由于在函数调用中使用变量
x
引起的,它干扰了
group3_lst
中data.tables中的
x
列。使用不在这些数据表中的差异变量名称。它可以正常工作,例如使用
i
combine\u sub1\u tst
dt_lst_tst <- lapply(seq_along(dt_lst),
function(x)
dt_lst[[x]][, group:= group1[[x]]])

> dt_lst_tst
[[1]]
        a b group
1:  0.8428429 4     1
2:  0.2958355 3     1
3: -1.0520980 2     1
4:  0.9628192 1     1

[[2]]
         c d group
1: -0.05033855 a     2
2: -0.94065157 b     2
3:  1.20459624 c     2
4: -0.47791557 d     2
5: -0.30362496 e     2
> dput(combine_sub1)
list(structure(list(smp = 1:4, x = c(491, 491, 491, 491), y = c(798, 
798, 798, 798)), .Names = c("smp", "x", "y"), class = c("data.table", 
"data.frame"), row.names = c(NA, -4L), .internal.selfref = <pointer: 
0x2b859d8>), 
structure(list(smp = 1:6, x = c(650, 650, 650, 650, 650, 
650), y = c(437, 437, 437, 437, 437, 437)), .Names = c("smp", 
"x", "y"), class = c("data.table", "data.frame"), row.names = c(NA, 
-6L), .internal.selfref = <pointer: 0x2b859d8>), structure(list(
    smp = 1:5, x = c(480, 485, 540, 572, 589), y = c(462, 
    462, 455, 451, 450)), .Names = c("smp", "x", "y"), class = 
c("data.table", 
"data.frame"), row.names = c(NA, -5L), .internal.selfref = <pointer: 
0x2b859d8>))

> combine_sub1
[[1]]
   smp   x   y
1:   1 491 798
2:   2 491 798
3:   3 491 798
4:   4 491 798

[[2]]
   smp   x   y
1:   1 650 437
2:   2 650 437
3:   3 650 437
4:   4 650 437
5:   5 650 437
6:   6 650 437

[[3]]
   smp   x   y
1:   1 480 462
2:   2 485 462
3:   3 540 455
4:   4 572 451
5:   5 589 450

group3_lst <- list(1,2,3)

> group3_lst
[[1]]
[1] 1

[[2]]
[1] 2

[[3]]
[1] 3
> combine_sub1_tst <- lapply(seq_along(combine_sub1),
+ function(x)
+ combine_sub1[[x]][ , group := group3_lst[[x]]])
Error in group3_lst[[x]] : no such index at level 1