简体   繁体   中英

How to calculate the sum of certain rows of a data frame in R

https://i.stack.imgur.com/HiUcL.png

I have this data frame, and I want to calculate the sum of each year in UK column. Each year has 12 data points for each month.

How do I calculate the total value for each year in the dataframe?

You can use sqldf for sum. Assuming data frame name is df

install.packages(sqldf)
library(sqldf)
data <- sqldf("select year,month,
                     round(sum(UK),1) as Sum_UK,
                     from df
                     group by year,month")

Hope this will help,there are many other option also you can try like dplyr

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