Reputation: 389
I have been struggling with aggregation/sum in R for my data frame. My data is as follows:
hour Case Time
7 2 35
8 8 125
9 10 145
10 15 18
11 17 167
12 20 220
13 25 135
14 14 167
15 18 134
16 16 100
17 7 55
18 6 178
19 5 245
20 1 133
21 1 12
22 0 54
23 2 180
24 1 55
I simply want to replace the "case" and Time" values for row where "hour"==8 with sum of "case" and "Time" values for rows where "hour" <= 8 or "hours" > 18 . I tried conditional aggregate(), with(), colsums() functions in R but none of them worked. I wonder if anybody can give me a hint.
Thanks
Upvotes: 2
Views: 255
Reputation: 92282
You could try conditional colSums
while assigning by condition to your original data set
df[df$hour == 8, c("Case", "Time")] <- colSums(df[df$hour <= 8 | df$hour > 18, c("Case", "Time")])
Upvotes: 3