mercredi 19 mai 2021

User-written function to replace NA by 0 by for loop not working in r

[data.table] I have written a function like this to replace NA to 0 if a column is numeric

fn.naremove <- function(data){ 
for (i in 1: length(data)){
if (class(data[[i]]) %in% c("numeric", "interger", "interger64")) {
  print(data[, names(data[, i]) := replace(data[, i], is.na(data[, i]), 0)])
} 
else {
 print(data)
}}}

I have a sample data table like below

dt1<- data.table(C1= c(1, 5, 14, NA, 54), C2= c(9, NA, NA, 3, 42), C3= c(9, 7, 42, 87, NA))

if I use fn.naremove(dt1) it returns the error

Error in `[.data.table`(data, , i) : 
j (the 2nd argument inside [...]) is a single symbol but column name 'i' is not found. 
Perhaps you intended DT[, ..i]. This difference to data.frame is deliberate and explained in FAQ 1.1.

If I run the code with the actual column index, it runs smoothly and returns the result I wanted for column number 1:

dt1[, names(dt1[, 1]) := replace(dt1[, 1], is.na(dt1[, 1]), 0)]

  C1 C2 C3
1:  1  9  9
2:  5 NA  7
3: 14 NA 42
4:  0  3 87
5: 54 42 NA

Please tell me if I miss or did something wrong with my function. Thanks in advance!!

Aucun commentaire:

Enregistrer un commentaire