[R] group bunch of lines in a data.frame, an additional requirement
Marc Schwartz (via MN)
mschwartz at mn.rr.com
Wed Sep 13 19:33:12 CEST 2006
Try something like this:
# Initial data frame
> DF
V1 V2 V3 V4
1 A 1.0 200 ID1
2 A 3.0 800 ID1
3 A 2.0 200 ID1
4 B 0.5 20 ID2
5 B 0.9 50 ID2
6 C 5.0 70 ID1
# Now do the aggregation to get the means
DF.1 <- aggregate(DF[, 2:3], list(V1 = DF$V1), mean)
> DF.1
V1 V2 V3
1 A 2.0 400
2 B 0.7 35
3 C 5.0 70
# Now get the unique combinations of letters and IDs in DF
DF.U <- unique(DF[, c("V1", "V4")])
> DF.U
V1 V4
1 A ID1
4 B ID2
6 C ID1
# Now merge the two data frames together, matching the letters
DF.NEW <- merge(DF.1, DF.U, by = "V1")
> DF.NEW
V1 V2 V3 V4
1 A 2.0 400 ID1
2 B 0.7 35 ID2
3 C 5.0 70 ID1
See ?unique and ?merge for more information.
Also, for the sake of clarification, these are not matrices, but data
frames. A matrix may contain only one data type, whereas data frames are
specifically designed to contain multiple data types as you have here.
HTH,
Marc Schwartz
On Wed, 2006-09-13 at 17:38 +0100, Emmanuel Levy wrote:
> Thanks for pointing me out "aggregate", that works fine!
>
> There is one complication though: I have mixed types (numerical and character),
>
> So the matrix is of the form:
>
> A 1.0 200 ID1
> A 3.0 800 ID1
> A 2.0 200 ID1
> B 0.5 20 ID2
> B 0.9 50 ID2
> C 5.0 70 ID1
>
> One letter always has the same ID but one ID can be shared by many
> letters (like ID1)
>
> I just want to keep track of the ID, and get a matrix like:
>
> A 2.0 400 ID1
> B 0.7 35 ID2
> C 5.0 70 ID1
>
> Any idea on how to do that without a loop?
>
> Many thanks,
>
> Emmanuel
>
> On 9/12/06, Emmanuel Levy <emmanuel.levy at gmail.com> wrote:
> > Hello,
> >
> > I'd like to group the lines of a matrix so that:
> > A 1.0 200
> > A 3.0 800
> > A 2.0 200
> > B 0.5 20
> > B 0.9 50
> > C 5.0 70
> >
> > Would give:
> > A 2.0 400
> > B 0.7 35
> > C 5.0 70
> >
> > So all lines corresponding to a letter (level), become a single line
> > where all the values of each column are averaged.
> >
> > I've done that with a loop but it doesn't sound right (it is very
> > slow). I imagine there is a
> > sort of "apply" shortcut but I can't figure it out.
> >
> > Please note that it is not exactly a matrix I'm using, the function
> > "typeof" tells me it's a list, however I access to it like it was a
> > matrix.
> >
> > Could someone help me with the right function to use, a help topic or
> > a piece of code?
> >
> > Thanks,
> >
> > Emmanuel
> >
More information about the R-help
mailing list