[R] rowsum
xtracto
b2017700 at lhsdv.com
Wed Oct 20 11:42:35 CEST 2010
Hello,
I am trying to achieve something which I *think* is possible using rowsum,
but a little help should be useful:
Consider the following dataframe DF0:
A B C
89 1 140
89 06 20
89 29 137
89 52 13
89 57 10
89 97 23
89 1 37
89 1 12
89 1 3
52 1 11
52 1 31
52 1 16
52 1 6
52 1 10
52 1 13
52 1 10
52 1 25
52 1 2
52 59 38
52 97 75
57 1 14
57 1 13
57 1 14
57 114 12
57 1 23
57 06 26
I need create a new dataframe containing the sums of all the rows where B =
1 for the different values of A, keeping the rows with other B values the
same. That is, for this data sample, the result I expect is something like
this (the order of the rows does not matter):
A B C
89 1 192 #From adding up: [140 + 37 + 12 + 3]
89 06 20
89 29 137
89 52 13
89 57 10
89 97 23
52 1 124 # From adding up: [11 + 31 + 16 + 6 + 10 + 13 + 10 + 25 + 2]
52 59 38
52 97 75
57 1 64 #From adding up: [14 +13 +14 +23]
57 114 12
57 06 26
Now, I now it should be possible to first separate the data in two sets,
where
DF1 <- DF0[DF0$B != 1,]
DF2 <- DF0[DF0$B == 1,]
Then I should apply sumrow to DF2 with some "group" vector, but I do not
know where to go from here.
Can anyone help?
Thanks in advance!
--
View this message in context: http://r.789695.n4.nabble.com/rowsum-tp3003551p3003551.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list