Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace NA's in all data tables in a list

Tags:

r

lapply

I have a list containing many data tables. For each of these tables I would like to replaces the NA's with 0.

I know how to change the NA's for each data table separately, but is there a way to put this into one command, e.g., using lapply?

For example: li is a list containing two data table, dt1 and dt2.

li <- list(dt1 = data.table(name = c(4,5), age = c(12, NA)), dt2= data.table(name = c(43,245,243), age = c(354,NA,NA)));

Changing the NA's to 0 in one data.table works like a charm:

d <- "dt1";
li[[d]][is.na(li[[d]])]<-0;

Results in:

> li
$dt1
  name age
1:    4  12
2:    5   0
$dt2
name age
1:   43 354
2:  245  NA
3:  243  NA

But when I try:

test <- lapply(names(li), function(d) li[[d]][is.na(li[[d]])]<-0)

I get:

> test
[[1]]
[1] 0
[[2]]
[1] 0

Is there any way do this without using a loop over all data tables in my list?

like image 741
Nieke Aerts Avatar asked Dec 05 '22 02:12

Nieke Aerts


2 Answers

You just need to return the list element

lapply(names(li), function(d) { li[[d]][is.na(li[[d]])] <-0; li[[d]] })
#[[1]]
#   name age
#1:    4  12
#2:    5   0

#[[2]]
#   name age
#1:   43 354
#2:  245   0
#3:  243   0

You could also use:

lapply(li, function(d) { d[is.na(d)] <- 0; d })
like image 80
germcd Avatar answered Dec 18 '22 01:12

germcd


Another option:

library(dplyr)
lapply(li, function(x) { mutate_each(x, funs(replace(., is.na(.), 0))) })
like image 37
Steven Beaupré Avatar answered Dec 18 '22 01:12

Steven Beaupré