简体   繁体   中英

How to sum value of a column based on equality from another column and create a new dataframe

this is my first question on stackoverflow. I hope to describe precisely enough my problem. So I'm trying to sum the value of a column called 'Fraction' based on equality of another column called 'Seq'. My dataframe looks like:

Fraction Seq
0.0872 CASSFAVQGGETQYF
0.0105 CASSFAVQGGETQYF

The hopeful output matrix should looks like

Fraction Seq
0.0977 CASSFAVQGGETQYF

I would like a new dataframe to be created from this operation. I have tried to use ddply but nothing successful resulted from it. Thank you for your help.

You can achieve this in base R using aggregate

aggregate(Fraction ~ Seq, FUN = "sum", data = df)

Or in dplyr using group_by and summmarise

df %>% 
group_by(Seq) %>% 
summarize(Fraction = sum(Fraction))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM