Reputation: 451
Having this data frame:
lt vl
1 a 1.0
2 2.0
3 0.5
4 6.0
5 7.0
6 a 6.0
7 8.0
8 7.0
9 9.0
10 d 7.0
11 NA
12 0.5
13 8.0
14 5.0
15 g 6.0
16 7.0
lt<-c("a","","","","","a","","","","d","","","","","g","")
vl<-c(1,2,0.5,6,7,6,8,7,9,7,NA,0.5,8,5,6,7)
I want to sum each value in vl
between characters of lt
. The process has to be like this:
a=1+2+0.5+6+7
a=6+8+7+9
d=7+0.5+8+5
g=6+7
To finally obtain:
lt vl
a 16.5
a 30
d 20.5
g 13
Note: a
is different of a
I'm trying hard to solve this, but it is rather difficult for me.
Upvotes: 2
Views: 35
Reputation: 76402
You can do this with tapply
. The grouping factor is established with a standard cumsum
trick.
f <- cumsum(lt != "")
res <- tapply(vl, f, FUN = sum, na.rm = TRUE)
names(res) <- lt[lt != ""]
res
# a a d g
#16.5 30.0 20.5 13.0
Upvotes: 2