dhfly a=c("t","t","r","t","f","r","t","t","f") b=c(1:9) c=c(11:19) d=data.table(a,b,c) d a b c 1: t 1 11 2: t 2 12 3: r 3 13 4: t 4 14 5: f 5 15 6: r 6 16 7: t 7 17 8: t 8 18 9: f 9 19 d[(a=="f" & b>4) | a=="r", c:=-1*c] Warning message: In [.data.table(d, (a == "f" & b > 4) | a == "r", :=(c, -1 * : Coerced double RHS to integer to match the type of the target column (column 3 named 'c'). The RHS values contain no fractions so would be more efficiently created as integer. Consider using R's 'L' postfix (typeof(0L) vs typeof(0)) to create constants as integer and avoid this warning. Wrapping the RHS with as.integer() will avoid this warning too but it's better if possible to create the RHS as integer in the first place so that the cost of the coercion can be avoided. 结果是正确的,请问这种警告信息会不会影响后续的运算。这种报警是怎么引起的,能否消除?
tctcab dhfly 不会影响,大意是希望你将C列从数值型(有小数位)换成整型以提高效率。 里面也提了 要消除这个警告可以: c列整数加上L后缀 如0L 手动转化c列为整型 as.integer(c) (我什么都没做就是把英文翻了一遍所以好好学英语啊巴嘎…
dhfly tctcab 谢谢,确实是英文水平太差(高中-),年龄也太大。承蒙多次出手相助,再次感谢。另外,上边的数据框可否在一条语句里实现:d[b!=2] 然后d[(a=="f" & b>4) | a=="r"), c:=as.integer(-1*c)],即先删除b=2这行数据,然后再进行本题原操作,省掉一个中间变量。
Jiena 把 c=c(11:19) 写成 c=as.numeric( c(11:19) ) 就可以消除报警。 library(data.table) a=c("t","t","r","t","f","r","t","t","f") b=c(1:9) c=as.numeric( c(11:19) ) d=data.table(a,b,c) d d[(a=="f" & b>4) | a=="r", c:=-1*c]