我想知道导致以下行为的原因让我感到有些惊讶 - 我定义了一个数据表dt_3
,然后将dt_1
定义为等于dt_3
。当我使用set()
替换dt_1
中的行元素时,dt_3
的相应元素也发生了变化:
refcols=c("A","B")
dt_3 = data.table(A=c(1,1,3,5,6,7), B = c("x","y","z","q","w","e"), C = rep("NO",6))
dt_2 = data.table(A=c(3,5,7), B = c("z","q","x"), D=c(3,5,99))
dt_1 = dt_3
dt_3
A B C
1: 1 x NO
2: 1 y NO
3: 3 z NO
4: 5 q NO
5: 6 w NO
6: 7 e NO
for(j in refcols){
set(dt_1,2,j,dt_2[3,get(j)])
}
Warning messages:
1: In set(dt_1, 2, j, dt_2[3, get(j)]) :
Coerced i from numeric to integer. Please pass integer for efficiency; e.g., 2L rather than 2
2: In set(dt_1, 2, j, dt_2[3, get(j)]) :
Coerced i from numeric to integer. Please pass integer for efficiency; e.g., 2L rather than 2
dt_3
A B C
1: 1 x NO
2: 7 x NO
3: 3 z NO
4: 5 q NO
5: 6 w NO
6: 7 e NO
是什么导致了这种情况,是否有一种更简单的方法可以通过显式行索引为特定列进行分组?
答案 0 :(得分:1)
我们可以使用copy
,这样当我们替换一个数据集中的元素时,另一个不会使用dt_1<- copy(dt_3)
。变化
for(j in refcols){
set(dt_1, i=NULL, j=j, value=dt_2[[j]])
}
dt_1
# A B C
#1: 3 z NO
#2: 5 q NO
#3: 7 x NO
#4: 3 z NO
#5: 5 q NO
#6: 7 x NO
关于第二部分,行指数不是很清楚。如果它仅基于列索引
for(j in refcols){
set(dt_1, i=2L, j=j, value=dt_2[[j]][3])
}
dt_1
# A B C
#1: 1 x NO
#2: 7 x NO
#3: 3 z NO
#4: 5 q NO
#5: 6 w NO
#6: 7 e NO
如果&#34; A&#34;的第2行?和&#34; B&#34;列在&#39; dt_1&#39;应替换为第3行&#39; dt_2&#39;对于相应的列(基于&#39; refcols&#39;)
tf.string_input_producer