为什么此命令在手写时有效,但在从 data.table 中提取相同输入时却无效
Why does this command work when hand-written, but not when the same inputs are drawn from a data.table
总结: 我有两行代码使用 data.table 和 rbind 从一个 table 中获取任意行列表,将它们添加到另一个 table,然后从源 table 中删除这些行。我试图通过从第三个 table 的列中绘制这些输入(起点 table、终点 table、行列表)来使其工作。 IE 而不是(伪代码示例)table1<-rbind(table1, table2[list_of_rownums_to_copy])
我想让它像 table3$col1<-rbind(table3$col1, table3$col2[table3$col4])
一样干净地工作,其中 col1 和 col2 的每一行都包含其他 table 的名称,而 col4 包含行号列表。这样我就可以将命令包装在一个函数中,并使用 mapply
或类似的方法将 运行 自动化多次。
详细:
出于测试目的,我通过 dtF 创建了 tables dtA。它们都具有相同的格式,每个 table 的行数不同。对于 space,这里只有 table dtA 的前 6 行。其他 table 是相同的,他们只是在 "orig_table" 列而不是 "a" 中有他们的字母:
val orig_table ##dtA has "a" in this column, dtB has "b", etc
1 1 a
2 2 a
3 3 a
4 4 a
5 5 a
6 6 a
只要我手写所有内容并给出一个列表对象,以下两行代码就可以在任何两个选定的 table 上完美运行:
dtA<- rbind(dtA, dtB[list_of_row_numbers, ])
dtB<- dtB[ -list_of_row_numbers, ]
在这种情况下,dtA 是目的地 table,dtB 是起点 table,list_of_row_numbers 是一个列表对象,其中包含作为行号的随机数对于哪些行 select。 IE 列表 c(1, 3, 5)
会将第 1、3、5 行从 table dtB 添加到 table dtA。第二行使用相同的列表来确定之后要从 dtB 中删除哪些行。和"cut and paste".
基本一样
我所做的是创建另一个 table,命名为 dt1,其中每一行都是这两个命令的完整输入集。这是 head(dt1)
的输出以供视觉参考:
V1 V2 V3 V4
1: dtA dtB 5804 3500,44228,22805,47866,32495,69006,...
2: dtA dtC 5637 59773,55783,73482,84333,57466,88604,...
3: dtA dtD 7292 67684,90789,67507,32937,90235,83391,...
4: dtA dtE 3321 16810,12906,40822,40316,52624,85656,...
5: dtA dtF 4268 89944,22578,23585,95320,79005,63923,...
6: dtB dtA 3219 46716,54828,11475,29245,76940, 2535,...
第一行的内容对应我举的手写命令的例子。 V1 列的第 1 行是目标 table (dtA),第 1 列 V2 是原点 table (dtB),第 1 列 V4 是要处理的行的列表:
V1 V1 V2 V4
dtA<- rbind(dtA, dtB[list_of_row_numbers, ]) ## this copies rows from dtB to dtA
dtB<- dtB[ -list_of_row_numbers, ] ## this deletes them from dtB afterwards
我正在尝试让它接受直接来自 table dt1 的输入。最终目标是将这两行包装在一个函数中以与 mapply
一起使用,以便遍历整个 table dt1 执行每行中列出的输入的每个操作。
问题是我想不出执行此操作的语法。我试过简单地用 dt1$V1
代替 dtA
,如下所示,但它根本不起作用:
dt1$V1<- rbind(dt1$V1, dt1$V2[dt1[,V4])
dt1$V2<- dt1$V2[ -dt1[,V4], ]
我之前通过将输入包装在 eval(as.name(dt1$V1))
中解决了类似的问题,它得到了一个不同的命令来正确地将 row1 col1 解释为对象的名称而不仅仅是文本,但它在这里不起作用。
我们可以通过 rbinding
包含来自 'V2' 列
的对象的数据集遍历行和 assign
每行中的对象
for(i in seq_len(nrow(dt1))) {
assign(dt1$V1[i], rbind(get(dt1$V1[i]), get(dt1$V2[i])[dt1$V4[[i]],]))
assign(dt1$V2[i], get(dt1$V2[i])[-dt1$V4[[i]],])
}
列 'V1'、'V2' 将对象名称存储为字符串。为了更新全局环境中的那些对象,我们需要 assign
将对象设置为新值。例如
v1 <- 5
assign("v1", 10)
v1
#[1] 10
此外,由于对象名称是字符串,对于 return 来自该字符串的值,我们使用 get
get("v1")
#[1] 10
在循环中,我们动态地使用 assign
和 get
在每次迭代中更新全局环境中的那些对象
总结: 我有两行代码使用 data.table 和 rbind 从一个 table 中获取任意行列表,将它们添加到另一个 table,然后从源 table 中删除这些行。我试图通过从第三个 table 的列中绘制这些输入(起点 table、终点 table、行列表)来使其工作。 IE 而不是(伪代码示例)table1<-rbind(table1, table2[list_of_rownums_to_copy])
我想让它像 table3$col1<-rbind(table3$col1, table3$col2[table3$col4])
一样干净地工作,其中 col1 和 col2 的每一行都包含其他 table 的名称,而 col4 包含行号列表。这样我就可以将命令包装在一个函数中,并使用 mapply
或类似的方法将 运行 自动化多次。
详细: 出于测试目的,我通过 dtF 创建了 tables dtA。它们都具有相同的格式,每个 table 的行数不同。对于 space,这里只有 table dtA 的前 6 行。其他 table 是相同的,他们只是在 "orig_table" 列而不是 "a" 中有他们的字母:
val orig_table ##dtA has "a" in this column, dtB has "b", etc
1 1 a
2 2 a
3 3 a
4 4 a
5 5 a
6 6 a
只要我手写所有内容并给出一个列表对象,以下两行代码就可以在任何两个选定的 table 上完美运行:
dtA<- rbind(dtA, dtB[list_of_row_numbers, ])
dtB<- dtB[ -list_of_row_numbers, ]
在这种情况下,dtA 是目的地 table,dtB 是起点 table,list_of_row_numbers 是一个列表对象,其中包含作为行号的随机数对于哪些行 select。 IE 列表 c(1, 3, 5)
会将第 1、3、5 行从 table dtB 添加到 table dtA。第二行使用相同的列表来确定之后要从 dtB 中删除哪些行。和"cut and paste".
我所做的是创建另一个 table,命名为 dt1,其中每一行都是这两个命令的完整输入集。这是 head(dt1)
的输出以供视觉参考:
V1 V2 V3 V4
1: dtA dtB 5804 3500,44228,22805,47866,32495,69006,...
2: dtA dtC 5637 59773,55783,73482,84333,57466,88604,...
3: dtA dtD 7292 67684,90789,67507,32937,90235,83391,...
4: dtA dtE 3321 16810,12906,40822,40316,52624,85656,...
5: dtA dtF 4268 89944,22578,23585,95320,79005,63923,...
6: dtB dtA 3219 46716,54828,11475,29245,76940, 2535,...
第一行的内容对应我举的手写命令的例子。 V1 列的第 1 行是目标 table (dtA),第 1 列 V2 是原点 table (dtB),第 1 列 V4 是要处理的行的列表:
V1 V1 V2 V4
dtA<- rbind(dtA, dtB[list_of_row_numbers, ]) ## this copies rows from dtB to dtA
dtB<- dtB[ -list_of_row_numbers, ] ## this deletes them from dtB afterwards
我正在尝试让它接受直接来自 table dt1 的输入。最终目标是将这两行包装在一个函数中以与 mapply
一起使用,以便遍历整个 table dt1 执行每行中列出的输入的每个操作。
问题是我想不出执行此操作的语法。我试过简单地用 dt1$V1
代替 dtA
,如下所示,但它根本不起作用:
dt1$V1<- rbind(dt1$V1, dt1$V2[dt1[,V4])
dt1$V2<- dt1$V2[ -dt1[,V4], ]
我之前通过将输入包装在 eval(as.name(dt1$V1))
中解决了类似的问题,它得到了一个不同的命令来正确地将 row1 col1 解释为对象的名称而不仅仅是文本,但它在这里不起作用。
我们可以通过 rbinding
包含来自 'V2' 列
assign
每行中的对象
for(i in seq_len(nrow(dt1))) {
assign(dt1$V1[i], rbind(get(dt1$V1[i]), get(dt1$V2[i])[dt1$V4[[i]],]))
assign(dt1$V2[i], get(dt1$V2[i])[-dt1$V4[[i]],])
}
列 'V1'、'V2' 将对象名称存储为字符串。为了更新全局环境中的那些对象,我们需要 assign
将对象设置为新值。例如
v1 <- 5
assign("v1", 10)
v1
#[1] 10
此外,由于对象名称是字符串,对于 return 来自该字符串的值,我们使用 get
get("v1")
#[1] 10
在循环中,我们动态地使用 assign
和 get
在每次迭代中更新全局环境中的那些对象