Emmanuel Levy
2006-Sep-12 20:50 UTC
[R] Basic help needed: group bunch of lines in a list (matrix)
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
Sundar Dorai-Raj
2006-Sep-12 21:03 UTC
[R] Basic help needed: group bunch of lines in a list (matrix)
Emmanuel Levy said the following on 9/12/2006 3:50 PM:> 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 > > ______________________________________________ > R-help at stat.math.ethz.ch 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.Try aggregate: aggregate(x[1], x[2:3], mean) where `x' is your data.frame. --sundar
Apparently Analagous Threads
- group bunch of lines in a data.frame, an additional requirement
- Retrieve indexes of the "first occurrence of numbers" in an effective manner
- How to "flatten" a multidimensional array into a dataframe?
- Finding (swapped) repetitions of numbers pairs across two columns
- Idea/package to "linearize a curve" along the diagonal?