Creating a new list with aggregate of values of a column from another list

PLEASE HELP I have a named list of 152 elements(daily values), each having 12 columns, I wish to create a new list with the sum/aggregate of the values of the column 11 year-wise...(the 12th column in my original contains the year-this is the daily dataset of sediment at 152 stations)

The result should be

list A

$Station 1

year 1971, 1972,1973.....

x 1276,13698,216895

$Station 2

year 1971, 1972,1973.....

x 1278,1898,21625

Are you looking for something like this? I invented a small data set with only two stations and two years of each but the process should work on large data sets.

library(tidyverse)
DF1 <- data.frame(A = rnorm(8), Value = 1:8, Year = rep(c(1971,1972),4))
DF2 <- data.frame(A = rnorm(8), Value = 11:18, Year = rep(c(1971,1972),4))
MasterList = list(Station1 = DF1, Station2 = DF2)
MyFunc <- function(DF) {
   DF |> group_by(Year) |> summarize(Aggreg = sum(Value))
 }
SumList <- map(MasterList, MyFunc)
SumList
$Station1
# A tibble: 2 x 2
   Year Aggreg
  <dbl>  <int>
1  1971     16
2  1972     20

$Station2
# A tibble: 2 x 2
   Year Aggreg
  <dbl>  <int>
1  1971     56
2  1972     60

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.