Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sum by year in a row in a dataframe in r

Tags:

r

I have a dataframe with two columns (year and precipitation). In a single column, the year is listed such that it starts from 1900 and ends at 2014 and again starts with 1900. In another column I have precipitation value of the respective year. Now i want to add all the precipitation of 1900 as 1 value and 1901 as 1 to up to 2014. My data looks like:

Year    Precipitation

1900    4.826
1901    37.592
2014    14.224
1900    45.974
1901    46.228
2014    79.502
1900    52.578
1901    22.30
2014    15.25

The results should look like:

Year   Precipitation

1900   103.378
1901   106.12
2014   108.976

So far I wrote a code but it does not work, if anybody can fix it?

data=read.table('precipitation.csv',header=T,sep=',')
frame=data.frame(data)
cumcum=tapply(frame$Precipitation, cumsum(frame$year==1), FUN=sum, na.rm=TRUE)

Thanks

like image 380
Juvin Avatar asked Mar 31 '15 05:03

Juvin


People also ask

How do I sum a row of data in R?

Syntax: mutate(new-col-name = rowSums(.)) The rowSums() method is used to calculate the sum of each row and then append the value at the end of each row under the new column name specified. The argument . is used to apply the function over all the cells of the data frame.

How do I calculate total in data frame in R?

To find the total by year column in an R data frame, we can use aggregate function with sum function.

How do you calculate the sum of each row in a matrix in R?

To find the sum of row, columns, and total in a matrix can be simply done by using the functions rowSums, colSums, and sum respectively.

How do I get the sum of months in R?

You can use the floor_date() function from the lubridate package in R to quickly group data by month.


2 Answers

Try data.table

library(data.table)
frame=fread('precipitation.csv',header=TRUE,sep=',')    
frame[, sum(Precipitation), by = Year]
like image 138
vrajs5 Avatar answered Oct 06 '22 00:10

vrajs5


1 liner -- try:

aggregate(frame['Precipitation'], by=frame['Year'], sum)

Reference: Consolidate duplicate rows

like image 39
csiu Avatar answered Oct 05 '22 23:10

csiu