R - convert nan to 0 results in all 0's
问题 I have a data frame containing NaN's that I'd like to convert to 0's. I wrote a function that I think should work: fix_nan <- function(x){ return(x[is.nan(x)] <- 0) } And then I apply it to the data frame: train_e <- structure(list(pack_id = structure(1:10, .Label = c("1", "2", "4", "5", "7", "8", "9", "10", "11", "14"), class = "factor"), item_1 = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0), item_2 = c(NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN), item_3 = c(1.45225232891169, 0.613104472886409, NaN