Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Sum the values of big amount columns based on one column

I have the dataframe below and I would like to sum the values of all my columns which actually are much more than 4, per Category. So I would like a loop or something to run something like:

library(dplyr)
Ind<-Abstract_50 %>% group_by(Category) %>%
  summarize(Abstract_50$`2018-03` = sum(Abstract_50$`2018-03`))

Abstract_50<-structure(list(Category = c("Gaming", "Comedy", "Education", 
"Entertainment", "People & Blogs", "Howto & Style", "Gaming", 
"Entertainment", "Comedy", "Education", "Science & Technology", 
"People & Blogs", "Gaming", "Howto & Style", "Entertainment", 
"Entertainment", "Entertainment", "Gaming", "Entertainment", 
"Gaming", "Entertainment", "Entertainment", "Film & Animation", 
"Entertainment", "Education", "Gaming", "Gaming", "People & Blogs", 
"Entertainment", "Comedy", "Gaming", "People & Blogs", "Entertainment", 
"Comedy", "Entertainment", "Sports", "Gaming", "Gaming", "Film & Animation", 
"Entertainment", "Entertainment", "Entertainment", "Music", "Gaming", 
"Film & Animation", "People & Blogs", "Entertainment", "Science & Technology", 
"Entertainment", "Gaming"), `2018-03` = c(NA, NA, 101163664, 
222435612, 2566, 539836128, 170830842, 43657714, NA, 589789888, 
18453451, 22327, 275987966, 3398517, 115637850, NA, 3358627, 
104063416, 904737659, NA, 138065088, NA, 178509153, 147605809, 
252376836, 47241577, 145748966, NA, 112800, NA, 92281120, NA, 
884076, NA, 147558050, 160818771, 209932596, 1269924, 66883227, 
NA, NA, NA, 45831420, 10426490, NA, NA, 67836946, 8847841, 2614794, 
51696616), `2018-04` = c(NA, NA, 138215454, 217690060, 2486, 
634144976, 140351664, 40516483, NA, 755403217, 23270005, -178648, 
326352497, 3315085, 77217671, NA, 2824360, 105855275, 708273455, NA, 209272453, NA, 165972373, 195448116, 262205166, 71156940, 
64229826, NA, 789991, NA, 107928087, NA, 587108, NA, 135852933, 
208830991, 198161237, 2462926, 77881362, NA, NA, NA, 38904148, 
21032898, NA, NA, 59783457, 16303243, 7311107, 57273819), `2018-05` = c(NA, 
NA, 151697886, 254479194, 2374, 712486430, 196327850, 75886076, 
NA, 959619148, 29213361, 164395, 330922933, 3112488, 86213061, 
NA, 3601193, 103629125, 708728068, NA, 250967293, NA, 151501861, 
48698231, 249038679, 73774699, 164568363, NA, 943891, NA, 130376287, 
NA, 5322697, NA, 164697288, 189706047, 213415764, 7806720, 78552229, 
NA, NA, NA, 73012836, 45648322, NA, NA, 68432350, 18830495, 15834552, 
88757967), `2018-06` = c(NA, NA, 142169001, 297190999, 2057, 
850709260, 191765278, 70501496, NA, 1454871272, 28011897, 465378, 
370654981, 3211888, 53592160, NA, 4667279, 193768594, 821707471, 
NA, 599954205, NA, 189548519, 371602705, 229237800, 68325636, 
193613716, NA, 1858138, NA, 94145274, NA, 8718655, NA, 255038043, 
254772059, 265200729, 9386440, 51753755, NA, NA, NA, 84645886, 
266089027, NA, NA, 167742900, 11844037, 33770300, 81240819)), row.names = c(NA, 
-50L), class = c("tbl_df", "tbl", "data.frame"))

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

You can use across to calculate the sum across all columns, except for the grouping column.

library(tidyverse)

Abstract_50 %>% 
  group_by(Category) %>% 
  summarise(across(everything(), sum))

Output

   Category             `2018-03`  `2018-04`  `2018-05`  `2018-06`
   <chr>                    <dbl>      <dbl>      <dbl>      <dbl>
 1 Comedy                      NA         NA         NA         NA
 2 Education            943330388 1155823837 1360355713 1826278073
 3 Entertainment               NA         NA         NA         NA
 4 Film & Animation            NA         NA         NA         NA
 5 Gaming                      NA         NA         NA         NA
 6 Howto & Style        543234645  637460061  715598918  853921148
 7 Music                 45831420   38904148   73012836   84645886
 8 People & Blogs              NA         NA         NA         NA
 9 Science & Technology  27301292   39573248   48043856   39855934
10 Sports               160818771  208830991  189706047  254772059
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading