Proof of last assertion - sorry, I just had to follow through... library(data.table) DT0 <- data.table(DF0, key = 'A, B') DT0[, sum(C), by = 'A, B'] A B V1 [1,] 52 1 124 [2,] 52 59 38 [3,] 52 97 75 [4,] 57 1 64 [5,] 57 6 26 [6,] 57 114 12 [7,] 89 1 192 [8,] 89 6 20 [9,] 89 29 137 [10,] 89 52 13 [11,] 89 57 10 [12,] 89 97 23 > aggregate(C ~ A + B, data = DF0, FUN = sum) A B C 1 52 1 124 2 57 1 64 3 89 1 192 4 57 6 26 5 89 6 20 6 89 29 137 7 89 52 13 8 89 57 10 9 52 59 38 10 52 97 75 11 89 97 23 12 57 114 12 > library(doBy) > summaryBy(C ~ A + B, data = DF0, FUN = sum) A B C.sum 1 52 1 124 2 52 59 38 3 52 97 75 4 57 1 64 5 57 6 26 6 57 114 12 7 89 1 192 8 89 6 20 9 89 29 137 10 89 52 13 11 89 57 10 12 89 97 23
That should give you enough options :) Dennis On Wed, Oct 20, 2010 at 3:24 AM, Dennis Murphy <djmu...@gmail.com> wrote: > Or even better (doh!)... > > library(plyr) > > ddply(DF0, .(A, B), summarise, C = sum(C)) > > A B C > 1 52 1 124 > 2 52 59 38 > 3 52 97 75 > 4 57 1 64 > 5 57 6 26 > 6 57 114 12 > 7 89 1 192 > 8 89 6 20 > 9 89 29 137 > 10 89 52 13 > 11 89 57 10 > 12 89 97 23 > > which means that aggregate(), summaryBy() in the doBy package and several > more functions/packages can do this quite easily. > > Dennis > > > On Wed, Oct 20, 2010 at 3:21 AM, Dennis Murphy <djmu...@gmail.com> wrote: > >> Hi: >> >> Here's one way, although it can be improved a bit. >> >> d1 <- aggregate(C ~ A, data = subset(DF0, B == 1), FUN = sum) >> d2 <- subset(DF0, B != 1) >> # B not in d1, so need to replace it >> > d1 >> A C >> 1 52 124 >> 2 57 64 >> 3 89 192 >> d1$B <- rep(1, nrow(d1)) >> d1 <- d1[, c(1, 3, 2)] # reorder columns to permit cbinding >> DF1 <- rbind(d1, d2) >> > DF1[order(DF1$A, DF1$B), ] >> A B C >> 1 52 1 124 >> 19 52 59 38 >> 20 52 97 75 >> 2 57 1 64 >> 26 57 6 26 >> 24 57 114 12 >> 3 89 1 192 >> 21 89 6 20 >> 31 89 29 137 >> 4 89 52 13 >> 5 89 57 10 >> 6 89 97 23 >> >> HTH, >> Dennis >> >> >> On Wed, Oct 20, 2010 at 2:42 AM, xtracto <b2017...@lhsdv.com> wrote: >> >>> >>> 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. >>> >>> ______________________________________________ >>> R-help@r-project.org mailing list >>> https://stat.ethz.ch/mailman/listinfo/r-help >>> PLEASE do read the posting guide >>> http://www.R-project.org/posting-guide.html >>> and provide commented, minimal, self-contained, reproducible code. >>> >> >> > [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.