I have a list of datasets.
dfList <- list(df1,df2,df3)
Each dataset looks like this.
apples, oranges
1,      2
NA,     4
I want to programatically change each dataframe's NAs to 0s. How do I do this?
My code so far...
lapply(
  X = dfList,
  FUN = cbind,
  is.na = FALSE
)
We can use replace
dfList1 <- lapply(dfList, function(x) replace(x, is.na(x), 0))
dfList1
#[[1]]
#  apples oranges
#1      1       2
#2      0       4
#[[2]]
#  apples oranges
#1      1       2
#2      0       4
#[[3]]
#  apples oranges
#1      1       2
#2      0       4
df2 <- df1
df3 <- df1
dfList1 <- list(df1, df2, df3)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With