-3

I know this has been asked before, but the answers are always too specific to the OP. I am also somewhat new to this.

I have a list WaFrames of 180 data frames. I need to affect them all because manually takes far too long.

The basic df[is.na(df)] <- 0works fine on one frame but I'm stuck when i'm trying to get it to work over every frame in the list. I've tried all the lapply ways and tried for loops. I'm stumped.

Also an answer could lead me to how to make other sweeping changes to all the frames at once.

Thanks in advance!

  • 3
    Try `lapply(WaFrames, function(x) replace(x, is.na(x), 0))` – akrun Feb 22 '18 at 18:18
  • 1
    Welcome to StackOverflow! Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610). This will make it much easier for others to help you. – Jaap Feb 22 '18 at 18:19
  • @akrun Thanks! This did loop through and didn’t give an error, but it didn’t make the change. I’m wondering if I need to stick it in something like for (i in WaFrames). – Kootseeahknee Feb 22 '18 at 19:14
  • You need to assign it i.t. `waFrames[] <- lapply(WaFrames, ...` – akrun Feb 23 '18 at 03:47
  • Thank you, again! That is a huge face palm moment. This worked. – Kootseeahknee Feb 23 '18 at 23:58

1 Answers1

2

You could do something like this:

x <- dplyr::data_frame(a = c(NA, 1, 2, NA),
                       b = c(1, 2, NA, 3))
y <- dplyr::data_frame(a = c(NA, 3, 4, NA),
                       b = c(1, 2, NA, 3))

listy <- list(x, y)
func <- function(df){
  df[is.na(df)] <- 0
}
purrr::map(listy, function(x) replace(x, is.na(x), 0))
RobertMyles
  • 2,673
  • 3
  • 30
  • 45