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
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.
To find the total by year column in an R data frame, we can use aggregate function with sum function.
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.
You can use the floor_date() function from the lubridate package in R to quickly group data by month.
Try data.table
library(data.table)
frame=fread('precipitation.csv',header=TRUE,sep=',')
frame[, sum(Precipitation), by = Year]
1 liner -- try:
aggregate(frame['Precipitation'], by=frame['Year'], sum)
Reference: Consolidate duplicate rows
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With