Squan Schmaan
Squan Schmaan

Reputation: 385

iteratively mutate nested dataframes

I have a list of dataframes with identical column names. I'd like to iterate through these dataframes, make some alteration to one of the columns, then apply that change back to the original dataframe. I know that doing this via iteration is not ideal, so if there's a better way using lapply, please educate me!

df <- list(
       '2020-01-01' = data.frame(value = c(3,5,7,8),
                  wt = c(1,1,1,1)),
       '2020-02-01' = data.frame(value = c(90,90,98,91),
                  wt = c(1,1,1,1)),
       '2020-03-01' = data.frame(value = c(0,10,3,8),
                  wt = c(1,1,1,1)))

for(i in df){

  data <- data.frame(
                 val = i$value,
                 wt = i$wt) %>%
  mutate(wt = ifelse(val > 95 | val < 5, 100, 1))

  # mutation works within the loop
  print(data)

  # But this bit does nothing
  i = data
}

print(df)

It seems that i = data is doing nothing. But if I do the same thing outside the loop, replacing i with a particular dataframe: df$'2020-01-01' it works fine:

data <- data.frame(val = df$`2020-01-01`$value,
               wt = df$`2020-01-01`$wt) %>% 
  mutate(wt = ifelse(val > 95 | val < 5, 100, 1))

df$`2020-01-01` = data

df

Upvotes: 0

Views: 36

Answers (1)

stefan
stefan

Reputation: 125318

Using lapply or purrr::map you could do:

library(dplyr)

df <- lapply(df, function(x) mutate(x, wt = if_else(value > 95 | value < 5, 100, 1)))
df
#> $`2020-01-01`
#>   value  wt
#> 1     3 100
#> 2     5   1
#> 3     7   1
#> 4     8   1
#> 
#> $`2020-02-01`
#>   value  wt
#> 1    90   1
#> 2    90   1
#> 3    98 100
#> 4    91   1
#> 
#> $`2020-03-01`
#>   value  wt
#> 1     0 100
#> 2    10   1
#> 3     3 100
#> 4     8   1

Upvotes: 1

Related Questions