Lukeul
Lukeul

Reputation: 1

Replace values for specific variables within dataframes contained in a list - R

I have a large number of dataframes contained in a list. I wish to replace specific values (0's) with NA but only for specific variables across all dataframes within the list.

I've tried the two pieces of code below, as well as several variations of mutate and rapply but alas no luck.

First piece of code and the error message:

a <- lapply(list,function(x) ifelse(x, var==0,NA))
 "Error in ifelse(x, var == 0, NA) : 
  'list' object cannot be coerced to type 'logical'"

Second piece of code - does not return an error message, however, replaces all 0's to NA instead of specific variables.

a <- lapply(list, function(x) na_if(list[[1]],0))

Can anybody provide any assistance before I blow my brains out.

Many thanks

Upvotes: 0

Views: 79

Answers (1)

langtang
langtang

Reputation: 24722

If your dataframes are contained in a list called dfs, then you can do something like this:

library(dplyr)

dfs <- lapply(dfs, \(df) df %>% mutate(x=if_else(x==0, as.double(NA), x)))

Input:

dfs = list(
  df1 = data.frame(x=c(0,4,3), y=c(1,2,3)),
  df2 = data.frame(x=c(0,0,1), y=c(0,0,6))
)

Output:

   x y
1 NA 1
2  4 2
3  3 3

$df2
   x y
1 NA 0
2 NA 0
3  1 6

If you had a set of columns for which you wanted to do this (i.e. not just column x, but column y also), you could do:

lapply(dfs, \(df) df %>% mutate(across(c(x,y), ~if_else(.x==0, as.double(NA), .x))))

OR

cols = c("x","y")
lapply(dfs, \(df) df %>% mutate(across(all_of(cols), ~if_else(.x==0, as.double(NA), .x))))

Upvotes: 1

Related Questions