0

In R data.table, how to replace NA value more efficiently ? Thanks!

test_data <- data.table(x=c('a','b','c'),y=c('d','e', NA),a=c(1:3),b=c(1,7,NA),c=c(NA,3,NA))

below code can work but very slow , because the actual data is more than 3 million rows

  test_data %>% mutate(across(where(is.numeric),function(x) str_replace_na(x,0)))
anderwyang
  • 1,801
  • 4
  • 18
  • 1
    What about something simple, that works with data.tables or data.frames?: `test_data <- replace(test_data, is.na(test_data), 0)` – Marc in the box Sep 14 '22 at 07:45

0 Answers0