0

I have tried both solutions from Replacing all missing values in R data.table with a value, but I am not able to find tmpd.

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.

What changes need to be made to the code to get it to work?

Thank you.

Community
  • 1
  • 1
iembry
  • 962
  • 1
  • 7
  • 23

1 Answers1

1

You need another nested ifelse to handle the NAs

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)]
Mike.Gahan
  • 4,565
  • 23
  • 39