Creating sum of many columns from names in one column [duplicate]





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1
















This question already has an answer here:




  • Aggregate multiple columns at once [duplicate]

    2 answers



  • Aggregating rows for multiple columns in R [duplicate]

    3 answers



  • Aggregate / summarize multiple variables per group (e.g. sum, mean)

    6 answers




I have a large data frame where I have one column (Phylum) that has repeated names and 253 other columns (each with a unique name) that have counts of the Phylum column. I would like to sum the counts within each column that correspond to each Phylum.



This is a simplified version of what my data look like:



     Phylum    sample1    sample2    sample3 ...    sample253
1 P1 2 3 5 5
2 P1 2 2 10 2
3 P2 1 0 0 1
4 P3 10 12 3 1
5 P3 5 7 14 15


I have seen similar questions, but they are for fewer columns, where you can just list the names of the columns you want summed. I don't want to enter 253 unique column names.



I would like my results to look like this



    Phylum    sample1    sample2    sample3 ...    sample253
1 P1 4 5 15 7
2 P2 1 0 0 1
3 P3 15 19 17 16


I would appreciate any help. Sorry for the format of the question, this is my first time asking for help on stackoverflow (rather than sleuthing).










share|improve this question













marked as duplicate by thelatemail r
Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 4 at 1:00


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • stackoverflow.com/questions/5963269/…

    – AidanGawronski
    Jan 4 at 0:51


















1
















This question already has an answer here:




  • Aggregate multiple columns at once [duplicate]

    2 answers



  • Aggregating rows for multiple columns in R [duplicate]

    3 answers



  • Aggregate / summarize multiple variables per group (e.g. sum, mean)

    6 answers




I have a large data frame where I have one column (Phylum) that has repeated names and 253 other columns (each with a unique name) that have counts of the Phylum column. I would like to sum the counts within each column that correspond to each Phylum.



This is a simplified version of what my data look like:



     Phylum    sample1    sample2    sample3 ...    sample253
1 P1 2 3 5 5
2 P1 2 2 10 2
3 P2 1 0 0 1
4 P3 10 12 3 1
5 P3 5 7 14 15


I have seen similar questions, but they are for fewer columns, where you can just list the names of the columns you want summed. I don't want to enter 253 unique column names.



I would like my results to look like this



    Phylum    sample1    sample2    sample3 ...    sample253
1 P1 4 5 15 7
2 P2 1 0 0 1
3 P3 15 19 17 16


I would appreciate any help. Sorry for the format of the question, this is my first time asking for help on stackoverflow (rather than sleuthing).










share|improve this question













marked as duplicate by thelatemail r
Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 4 at 1:00


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















  • stackoverflow.com/questions/5963269/…

    – AidanGawronski
    Jan 4 at 0:51














1












1








1









This question already has an answer here:




  • Aggregate multiple columns at once [duplicate]

    2 answers



  • Aggregating rows for multiple columns in R [duplicate]

    3 answers



  • Aggregate / summarize multiple variables per group (e.g. sum, mean)

    6 answers




I have a large data frame where I have one column (Phylum) that has repeated names and 253 other columns (each with a unique name) that have counts of the Phylum column. I would like to sum the counts within each column that correspond to each Phylum.



This is a simplified version of what my data look like:



     Phylum    sample1    sample2    sample3 ...    sample253
1 P1 2 3 5 5
2 P1 2 2 10 2
3 P2 1 0 0 1
4 P3 10 12 3 1
5 P3 5 7 14 15


I have seen similar questions, but they are for fewer columns, where you can just list the names of the columns you want summed. I don't want to enter 253 unique column names.



I would like my results to look like this



    Phylum    sample1    sample2    sample3 ...    sample253
1 P1 4 5 15 7
2 P2 1 0 0 1
3 P3 15 19 17 16


I would appreciate any help. Sorry for the format of the question, this is my first time asking for help on stackoverflow (rather than sleuthing).










share|improve this question















This question already has an answer here:




  • Aggregate multiple columns at once [duplicate]

    2 answers



  • Aggregating rows for multiple columns in R [duplicate]

    3 answers



  • Aggregate / summarize multiple variables per group (e.g. sum, mean)

    6 answers




I have a large data frame where I have one column (Phylum) that has repeated names and 253 other columns (each with a unique name) that have counts of the Phylum column. I would like to sum the counts within each column that correspond to each Phylum.



This is a simplified version of what my data look like:



     Phylum    sample1    sample2    sample3 ...    sample253
1 P1 2 3 5 5
2 P1 2 2 10 2
3 P2 1 0 0 1
4 P3 10 12 3 1
5 P3 5 7 14 15


I have seen similar questions, but they are for fewer columns, where you can just list the names of the columns you want summed. I don't want to enter 253 unique column names.



I would like my results to look like this



    Phylum    sample1    sample2    sample3 ...    sample253
1 P1 4 5 15 7
2 P2 1 0 0 1
3 P3 15 19 17 16


I would appreciate any help. Sorry for the format of the question, this is my first time asking for help on stackoverflow (rather than sleuthing).





This question already has an answer here:




  • Aggregate multiple columns at once [duplicate]

    2 answers



  • Aggregating rows for multiple columns in R [duplicate]

    3 answers



  • Aggregate / summarize multiple variables per group (e.g. sum, mean)

    6 answers








r






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 4 at 0:46









KhadLilyKhadLily

83




83




marked as duplicate by thelatemail r
Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 4 at 1:00


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by thelatemail r
Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 4 at 1:00


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • stackoverflow.com/questions/5963269/…

    – AidanGawronski
    Jan 4 at 0:51



















  • stackoverflow.com/questions/5963269/…

    – AidanGawronski
    Jan 4 at 0:51

















stackoverflow.com/questions/5963269/…

– AidanGawronski
Jan 4 at 0:51





stackoverflow.com/questions/5963269/…

– AidanGawronski
Jan 4 at 0:51












1 Answer
1






active

oldest

votes


















0














If your starting file looks like this (test.csv):



Phylum,sample1,sample2,sample3,sample253
P1,2,3,5,5
P1,2,2,10,2
P2,1,0,0,1
P3,10,12,3,1
P3,5,7,14,15


Then you can use group_by and summarise_each from dplyr:



read_csv('test.csv') %>% 
group_by(Phylum) %>%
summarise_each(funs(sum))


(I first loaded tidyverse with library(tidyverse).)



Note that, if you were trying to do this for one column you can simply use summarise:



read_csv('test.csv') %>% 
group_by(Phylum) %>%
summarise(sum(sample1))


summarise_each is required to run that function (in the above, funs(sum)) on each column.






share|improve this answer


























  • I received the error: summarise_each() is deprecated. So instead I used summarise_all() and it seemed to work! Thank you :)

    – KhadLily
    Jan 4 at 1:34













  • Oh I didn't notice that error summarise_each()` is deprecated. Use `summarise_all()`, `summarise_at()` or `summarise_if()` instead. To map `funs` over all variables, use `summarise_all() ... thanks for pointing that out :)

    – snd
    Jan 4 at 1:55


















1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














If your starting file looks like this (test.csv):



Phylum,sample1,sample2,sample3,sample253
P1,2,3,5,5
P1,2,2,10,2
P2,1,0,0,1
P3,10,12,3,1
P3,5,7,14,15


Then you can use group_by and summarise_each from dplyr:



read_csv('test.csv') %>% 
group_by(Phylum) %>%
summarise_each(funs(sum))


(I first loaded tidyverse with library(tidyverse).)



Note that, if you were trying to do this for one column you can simply use summarise:



read_csv('test.csv') %>% 
group_by(Phylum) %>%
summarise(sum(sample1))


summarise_each is required to run that function (in the above, funs(sum)) on each column.






share|improve this answer


























  • I received the error: summarise_each() is deprecated. So instead I used summarise_all() and it seemed to work! Thank you :)

    – KhadLily
    Jan 4 at 1:34













  • Oh I didn't notice that error summarise_each()` is deprecated. Use `summarise_all()`, `summarise_at()` or `summarise_if()` instead. To map `funs` over all variables, use `summarise_all() ... thanks for pointing that out :)

    – snd
    Jan 4 at 1:55
















0














If your starting file looks like this (test.csv):



Phylum,sample1,sample2,sample3,sample253
P1,2,3,5,5
P1,2,2,10,2
P2,1,0,0,1
P3,10,12,3,1
P3,5,7,14,15


Then you can use group_by and summarise_each from dplyr:



read_csv('test.csv') %>% 
group_by(Phylum) %>%
summarise_each(funs(sum))


(I first loaded tidyverse with library(tidyverse).)



Note that, if you were trying to do this for one column you can simply use summarise:



read_csv('test.csv') %>% 
group_by(Phylum) %>%
summarise(sum(sample1))


summarise_each is required to run that function (in the above, funs(sum)) on each column.






share|improve this answer


























  • I received the error: summarise_each() is deprecated. So instead I used summarise_all() and it seemed to work! Thank you :)

    – KhadLily
    Jan 4 at 1:34













  • Oh I didn't notice that error summarise_each()` is deprecated. Use `summarise_all()`, `summarise_at()` or `summarise_if()` instead. To map `funs` over all variables, use `summarise_all() ... thanks for pointing that out :)

    – snd
    Jan 4 at 1:55














0












0








0







If your starting file looks like this (test.csv):



Phylum,sample1,sample2,sample3,sample253
P1,2,3,5,5
P1,2,2,10,2
P2,1,0,0,1
P3,10,12,3,1
P3,5,7,14,15


Then you can use group_by and summarise_each from dplyr:



read_csv('test.csv') %>% 
group_by(Phylum) %>%
summarise_each(funs(sum))


(I first loaded tidyverse with library(tidyverse).)



Note that, if you were trying to do this for one column you can simply use summarise:



read_csv('test.csv') %>% 
group_by(Phylum) %>%
summarise(sum(sample1))


summarise_each is required to run that function (in the above, funs(sum)) on each column.






share|improve this answer















If your starting file looks like this (test.csv):



Phylum,sample1,sample2,sample3,sample253
P1,2,3,5,5
P1,2,2,10,2
P2,1,0,0,1
P3,10,12,3,1
P3,5,7,14,15


Then you can use group_by and summarise_each from dplyr:



read_csv('test.csv') %>% 
group_by(Phylum) %>%
summarise_each(funs(sum))


(I first loaded tidyverse with library(tidyverse).)



Note that, if you were trying to do this for one column you can simply use summarise:



read_csv('test.csv') %>% 
group_by(Phylum) %>%
summarise(sum(sample1))


summarise_each is required to run that function (in the above, funs(sum)) on each column.







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 4 at 1:01

























answered Jan 4 at 0:55









sndsnd

73811129




73811129













  • I received the error: summarise_each() is deprecated. So instead I used summarise_all() and it seemed to work! Thank you :)

    – KhadLily
    Jan 4 at 1:34













  • Oh I didn't notice that error summarise_each()` is deprecated. Use `summarise_all()`, `summarise_at()` or `summarise_if()` instead. To map `funs` over all variables, use `summarise_all() ... thanks for pointing that out :)

    – snd
    Jan 4 at 1:55



















  • I received the error: summarise_each() is deprecated. So instead I used summarise_all() and it seemed to work! Thank you :)

    – KhadLily
    Jan 4 at 1:34













  • Oh I didn't notice that error summarise_each()` is deprecated. Use `summarise_all()`, `summarise_at()` or `summarise_if()` instead. To map `funs` over all variables, use `summarise_all() ... thanks for pointing that out :)

    – snd
    Jan 4 at 1:55

















I received the error: summarise_each() is deprecated. So instead I used summarise_all() and it seemed to work! Thank you :)

– KhadLily
Jan 4 at 1:34







I received the error: summarise_each() is deprecated. So instead I used summarise_all() and it seemed to work! Thank you :)

– KhadLily
Jan 4 at 1:34















Oh I didn't notice that error summarise_each()` is deprecated. Use `summarise_all()`, `summarise_at()` or `summarise_if()` instead. To map `funs` over all variables, use `summarise_all() ... thanks for pointing that out :)

– snd
Jan 4 at 1:55





Oh I didn't notice that error summarise_each()` is deprecated. Use `summarise_all()`, `summarise_at()` or `summarise_if()` instead. To map `funs` over all variables, use `summarise_all() ... thanks for pointing that out :)

– snd
Jan 4 at 1:55





Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas