SpinningAtInfinity SpinningAtInfinity - 9 days ago 4x
R Question

Create a new column called `daily` which is the sum of the 24 hourly columns

Problem : Create a new column called
which is the sum of the 24 hourly columns.

I assign the data set in rnf6080.dat (available at www.stats.uwo.ca/faculty/braun/data/rnf6080.dat) to a data frame called
. I re-labelled the columns of this data frame :
names(rain) <- c("year", "month", "day", seq(0,23))
. So far I tried
df <- c(rain$0, rain$1, rain$2, rain$3, rain$4, rain$5, rain$6, rain$7, rain$8, rain$9, rain$10, rain$11, rain$12, rain$13, rain$14, rain$15, rain$16, rain$17, rain$18, rain$19, rain$20, rain$21, rain$22, rain$23)
rain["daily"] <- colSums(df)
. I got
Error in colSums(df) : 'x' must be an array of at least two dimensions
. In fact, I know what to do, but I don't know how to do it. I know I have to sum vector to get
, but I'm confused with how to do it.

Question : Could anyone be able to improve and tell me what I could modify to fix my problem?


If you'd like to sum across the columns you've labeled as 0-23, the below code will work.

rain.df$daily <- rowSums(rain.df[,4:27])