Home > Net >  Summarizing Mulitple Colums for Multiple Dataframes with Same Columns
Summarizing Mulitple Colums for Multiple Dataframes with Same Columns

Time:05-20

So I have 7 dataframes with the exact same datastructure:

# A tibble: 6 x 25
  Full.Name `1_2019` `1_2020` `10_2019` `10_2020` `11_2019` `11_2020` `12_2019` `12_2020` `2_2019` `2_2020` `3_2019` `3_2020` `4_2019`
  <chr>        <dbl>    <dbl>     <dbl>     <dbl>     <dbl>     <dbl>     <dbl>     <dbl>    <dbl>    <dbl>    <dbl>    <dbl>    <dbl>
1 A. Patri~   0.0108  0.00909    0.0121    0.0181    0.0112    0.0197    0.0133    0.0164   0.0191   0.0188  0.0207    0.0196   0.0164
2 Aaron P.~   0       0          0         0         0         0         0         0        0        0       0         0        0.0714
3 Aaron P.~   0       0          0         0         0         0         0         0        0        0       0         0        0     
4 Adam H. ~   0       0          0         0         0         0.0227    0         0        0        0       0         0.0182   0     
5 Adam P. ~   0       0.0123     0.0159    0.04      0.0153    0         0         0        0.0294   0.0177  0.00820   0        0     
6 Adena T.~   0.0104  0.0148     0.0252    0.0270    0.0185    0.0349    0.0240    0.0370   0.0175   0       0.0134    0.0116   0.0142
# ... with 11 more variables: `4_2020` <dbl>, `5_2019` <dbl>, `5_2020` <dbl>, `6_2019` <dbl>, `6_2020` <dbl>, `7_2019` <dbl>,
#   `7_2020` <dbl>, `8_2019` <dbl>, `8_2020` <dbl>, `9_2019` <dbl>, `9_2020` <dbl>

all 7 dataframes got the same values for Full.Name and all of the columns are the same. The only differences are different values for the x_20xx columns.

I want to summarize them into a new dataframe, which adds up the values for each name-row and month-column. The new dataframe should have the same columns and the Full.Name column must be the exact same one. The other columns must be sums of all 7 dataframes.

Any help is appreciated. For experimental purposes, you can simply duplicate the provided dataframe to 7 dataframes.

the dput() output is the following:

structure(list(Full.Name = c("A. Patrick Beharelle", "Aaron P. Graft", 
"Aaron P. Jagdfeld", "Adam H. Schechter", "Adam P. Symson"), 
    `1_2019` = c(0.0107913669064748, 0, 0, 0, 0), `1_2020` = c(0.00909090909090909, 
    0, 0, 0, 0.0122699386503067), `10_2019` = c(0.0121212121212121, 
    0, 0, 0, 0.0158730158730159), `10_2020` = c(0.0181268882175227, 
    0, 0, 0, 0.04), `11_2019` = c(0.0111607142857143, 0, 0, 0, 
    0.0152671755725191), `11_2020` = c(0.0196779964221825, 0, 
    0, 0.0227272727272727, 0), `12_2019` = c(0.0133333333333333, 
    0, 0, 0, 0), `12_2020` = c(0.0163934426229508, 0, 0, 0, 0
    ), `2_2019` = c(0.0190641247833622, 0, 0, 0, 0.0294117647058824
    ), `2_2020` = c(0.0187793427230047, 0, 0, 0, 0.0176991150442478
    ), `3_2019` = c(0.0207006369426752, 0, 0, 0, 0.00819672131147541
    ), `3_2020` = c(0.0196078431372549, 0, 0, 0.0181818181818182, 
    0), `4_2019` = c(0.0164473684210526, 0.0714285714285714, 
    0, 0, 0), `4_2020` = c(0.0172413793103448, 0, 0, 0.0158730158730159, 
    0.0140845070422535), `5_2019` = c(0.0146252285191956, 0, 
    0, 0, 0.0222222222222222), `5_2020` = c(0.00623052959501558, 
    0, 0, 0.008, 0.00806451612903226), `6_2019` = c(0.0256410256410256, 
    0.0120481927710843, 0, 0.0434782608695652, 0.032258064516129
    ), `6_2020` = c(0.0300429184549356, 0, 0, 0, 0.0198019801980198
    ), `7_2019` = c(0.0107816711590297, 0, 0, 0, 0), `7_2020` = c(0.0108108108108108, 
    0, 0, 0.03125, 0), `8_2019` = c(0.0177514792899408, 0, 0, 
    0, 0.0306122448979592), `8_2020` = c(0.0149700598802395, 
    0, 0, 0.0212765957446809, 0.0909090909090909), `9_2019` = c(0.0146699266503667, 
    0, 0, 0.0555555555555556, 0.00917431192660551), `9_2020` = c(0.00738916256157635, 
    0.010989010989011, 0.2, 0, 0)), row.names = c(NA, -5L), class = c("tbl_df", 
"tbl", "data.frame"))

Thanks in advance for every bit of help. I appreciate it.

CodePudding user response:

This should do it:

alldat <- bind_rows(dat1, dat2, dat3, dat4, 
                    dat5, dat6, dat7)
alldat %>% group_by(Full.Name) %>% 
  summarise(across(everything(), sum))
  • Related