[R] merging with aggregating

Marc Schwartz (via MN) mschwartz at mn.rr.com
Tue Dec 6 17:19:17 CET 2005


On Tue, 2005-12-06 at 15:19 +0100, Dubravko Dolic wrote:
> Hi all,
> 
> the moment you hit the 'send' button you know the answer...
> 
> I approached a solution similar to this one given by Marc. But maybe
> there is a better one? Even because this operation is done in a
> for-loop during which R gets new data from a database. So I sum up 16
> data.frames eventually.
> 
> Dubro

<SNIP>


OK....so here is one possible approach to a more generic solution:


# Preallocate a list with 16 elements

DF.List <- replicate(16, list(numeric(0)))


DF.List looks like:

> head(DF.List)
[[1]]
numeric(0)

[[2]]
numeric(0)

[[3]]
numeric(0)

[[4]]
numeric(0)

...


# Do your loop here, placing the actual results
# of your queries into DF.List[[i]]. I am just using
# random samples here for the example.
# NOTE: I am making the assumption in this example
# that each resultant DF will have the same structure.

for (i in 1:16)
{
  DF.List[[i]] <- data.frame(n = sample(20, 10),
                             V1 = sample(20, 10),
                             V2 = sample(0:10, 10))
}


# Now rbind() the data frames together
DF.All <- do.call("rbind", DF.List)

# Now do use aggregate() to get the sums of V1 and V2
# by 'n'.
DF.Sums <- aggregate(DF.All[, c("V1", "V2")], list(n = DF.All$n), sum)


> DF.Sums
    n  V1 V2
1   1 161 65
2   2  86 67
3   3  72 28
4   4  59 31
5   5 101 48
6   6  68 41
7   7  75 34
8   8  73 30
9   9  59 26
10 10  80 16
11 11 127 44
12 12 111 78
13 13 111 38
14 14  69 28
15 15  71 26
16 16  90 51
17 17  50 36
18 18  48 41
19 19  92 38
20 20  71 22


Does that get closer to what you need?

HTH,

Marc Schwartz




More information about the R-help mailing list