2014-07-25 33 views
0

我已经尝试了两种解决方案,从 Replacing all missing values in R data.table with a value,但我无法找到tmpdifelse data.table乘法,同时保持NA值为真

library(data.table) 
set.seed(10) 
datatmps <- data.table(ID = seq(11), A = c(0.32, sample(1:40, 10,)), B = c(NA, 4.3, 
32.21, -0.9832, NA, 45, 3, 2, 90, 109.3, NA), C = c(sample(1:30, 10,), -0.87)) 
setkey(datatmps, ID) 
for (i in seq_along(datatmps)) set(datatmps, i=which(is.na(datatmps[[i]])), j=i, 
value=NA_real_) 
tmpd <- datatmps[, lapply(.SD, function(x) { 
ifelse(x < 62.276, 6.107799961 + x * (0.4436518521 + x * (0.01428945805 + x * 
(0.0002650648471 + x * (0.000003031240396 + x * (0.00000002034080948 + x * 
0.00000000006136820929))))), 
-296.901212123675 + 16.9015967001546 * x - 0.302242100380422 * x^2 + 
0.00264123776535373 * x^3)}), by = key(datatmps)] 

# Error in `[.data.table`(datatmps, , lapply(.SD, function(x) { : 
# Column 2 of result for group 2 is type 'double' but expecting type 'logical'. 
# Column types must be consistent for each group. 

需要对代码进行哪些更改才能使其正常工作?

谢谢。

回答

1

你需要另一个嵌套ifelse处理NA小号

yourfun <- function(x) { 
    ifelse(x < 62.276, 6.107799961 + x * (0.4436518521 + x * (0.01428945805 + x * 
    (0.0002650648471 + x * (0.000003031240396 + x * (0.00000002034080948 + x * 
    0.00000000006136820929))))), 
    -296.901212123675 + 16.9015967001546 * x - 0.302242100380422 * x^2 + 
    0.00264123776535373 * x^3)} 

tmpd <- datatmps[, lapply(.SD, function(x) ifelse(!is.na(x),yourfun(x),NA_real_)), by = key(datatmps)] 
+0

谢谢麦克,你的推荐代码改进工作完美 – iembry