简体   繁体   English

我在做什么错(data.table,R)?

[英]What am I doing wrong (data.table, R)?

I'm currently doing some tests with the set function in the data.table package in R and have the following code: 我目前正在使用R中的data.table包中的set函数进行一些测试,并具有以下代码:

  dt= data.table(ans=rep(c(14,16),100))
  dt[,voy:=0.0]
  set(dt,which(dt[,ans]==14),"voy",log(dt[,ans]))
  dt

Note that I want to compute the logarithm of those cases having ans=14 using the set function, but I'm not getting the correct result. 请注意,我想使用set函数来计算ans=14的那些情况的对数,但是我没有得到正确的结果。 This is the result I got: 这是我得到的结果:

  ans      voy
  1:  14 2.639057
  2:  16 0.000000
  3:  14 2.772589
  4:  16 0.000000
  5:  14 2.639057
  ---             
  196:  16 0.000000
  197:  14 2.639057
  198:  16 0.000000
  199:  14 2.772589
  200:  16 0.000000

You may note that for some rows the value of the variable voy is the expected log(14)=2.639057 but for others cases having ans=14 it is assigned 2.772589=log(16) . 您可能会注意到,对于某些行,变量voy的值是预期的log(14)=2.639057但对于其他情况而言,其ans=14则被分配了2.772589=log(16) So, I think I'm misusing the set function. 所以,我想我滥用了set函数。 How can I solve this? 我该如何解决? I know the next code can be used to carry this out: 我知道下一个代码可以用来执行此操作:

dt[ans==14,voy:=log(ans)]

But I want to translate this into the set function syntax. 但我想将此转换为set函数语法。

You need to subset the data for the value parameter. 您需要为value参数的数据子集。 In your case, the warning Supplied 200 items to be assigned to 100 items of column 'voy' (100 unused) could have given you an idea. 在您的情况下,警告“已将200个项目分配给”航次”列中的100个项目(100个未使用)可能会给您一个想法。 You were picking one by one the first 100 values of dt$ans , which indeed are alternating 14's and 16's. 您正在dt$ans的前100个值中一个接一个地选择,实际上是14和16交替出现。

This way it works: 这样工作:

set(dt,which(dt[,ans]==14),"voy",log(dt[ans==14,ans]))

giving: 赠送:

     ans      voy
  1:  14 2.639057
  2:  16 0.000000
  3:  14 2.639057
  4:  16 0.000000
  5:  14 2.639057
 ---             
196:  16 0.000000
197:  14 2.639057
198:  16 0.000000
199:  14 2.639057
200:  16 0.000000

But it's ugly code, as @Andrie already remarked. 但这是丑陋的代码,就像@Andrie所说的那样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM