accessing columns of dataframe within a list

I have a list (np) with 152 dataframes (named elements which are stations), and each having two columns (Discharge and Sediment)
I want to fill the missing values using Kalman Arima function, but how to pass these columns as arguments to the function? I have tried a lot of ,[] and np[,] subsetting options, kindly help

List
np
$ Adityapur
Year Discharge Sediment

$ Alladupalli
Year Discharge Sediment

the code below replaces the missing values individually for a single station in the list np.
np$Alladupalli$Sediment<-na_kalman(np$Alladupalli$Sediment, model = "StructTS", smooth = TRUE, maxgap = Inf)

Here is an example where I act on a given column of every data frame stored in a list. I just did a simple multiplication but using na_kalman should be very similar. Just write

DF$Sediment <- na_kalman(DF$Sediment,  model = "StructTS", smooth = TRUE, maxgap = Inf)

instead of my multiplication step.

library(tidyverse)
#> Warning: package 'tibble' was built under R version 4.1.2
DF1 <- data.frame(Year = 2020:2022, 
                  Discharge = c(3,5,4), Sediment = c(5,7,6))
      
DF2 <- data.frame(Year = 2020:2022, 
                  Discharge = c(5,2,3), Sediment = c(6,4,7))

DF1 <- data.frame(Year = 2020:2022, 
                  Discharge = c(3,5,4), Sediment = c(5,7,6))
np = list(A = DF1, B = DF2)
np
#> $A
#>   Year Discharge Sediment
#> 1 2020         3        5
#> 2 2021         5        7
#> 3 2022         4        6
#> 
#> $B
#>   Year Discharge Sediment
#> 1 2020         5        6
#> 2 2021         2        4
#> 3 2022         3        7
ExmplFunc <- function(DF) {
  DF$Discharge <- DF$Discharge * 2
  return(DF)
}
np2 <- map(np, ExmplFunc)
np2
#> $A
#>   Year Discharge Sediment
#> 1 2020         6        5
#> 2 2021        10        7
#> 3 2022         8        6
#> 
#> $B
#>   Year Discharge Sediment
#> 1 2020        10        6
#> 2 2021         4        4
#> 3 2022         6        7

Created on 2022-03-28 by the reprex package (v2.0.1)

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.