Hi all, Imagine I have a matrix and the first colum is a list that repeats the same names, I want to sum the second column on each unique name on first column. Imagine this: Pepe 2 Pepe 3 Pepe 4 Jose 2 Jose 5 Manuel 4 Manuel 2 I want to make a new matrix that calculates and recognizes that there are 3 different names ans sum second column. But a priori I don´t know the list of the different names: In my example Pepe 9 Jose 7 Manuel 6 I´m trying to use something like sapply or apply but I can find the key... Can anyone help or guide me ? Thanks in advance¡¡¡ [[alternative HTML version deleted]]
Hi Jose,
Here is a suggestion using tapply():
R> x <- read.table(textConnection("Pepe 2
+ Pepe 3
+ Pepe 4
+ Jose 2
+ Jose 5
+ Manuel 4
+ Manuel 2"), header = FALSE)
R> closeAllConnections()
R> x
V1 V2
1 Pepe 2
2 Pepe 3
3 Pepe 4
4 Jose 2
5 Jose 5
6 Manuel 4
7 Manuel 2
R>
R>
R> with(x, tapply(V2, V1, sum))
Jose Manuel Pepe
7 6 9
HTH,
Jorge
On 12/11/2009 4:38 PM, Jose Narillos de Santos wrote:> Hi all,
>
> Imagine I have a matrix and the first colum is a list that repeats the same
> names, I want to sum the second column on each unique name on first column.
>
> Imagine this:
>
> Pepe 2
> Pepe 3
> Pepe 4
> Jose 2
> Jose 5
> Manuel 4
> Manuel 2
>
> I want to make a new matrix that calculates and recognizes that there are 3
> different names ans sum second column. But a priori I don´t know the list
of
> the different names:
>
> In my example
>
> Pepe 9
> Jose 7
> Manuel 6
>
> I´m trying to use something like sapply or apply but I can find the key...
>
> Can anyone help or guide me ?
>
> Thanks in advance¡¡¡
>
> [[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.
>
[[alternative HTML version deleted]]
On Dec 11, 2009, at 4:38 PM, Jose Narillos de Santos wrote:> Hi all, > > Imagine I have a matrix and the first colum is a list that repeats > the same > names, I want to sum the second column on each unique name on first > column. > > Imagine this: > > Pepe 2 > Pepe 3 > Pepe 4 > Jose 2 > Jose 5 > Manuel 4 > Manuel 2?tapply # or one of its derivative fucntions like by or aggregate> > I want to make a new matrix that calculates and recognizes that > there are 3 > different names ans sum second column. But a priori I don?t know the > list of > the different names: > > In my exampleSomething like: tapply(valuecol, namecol, sum) (Untested.)> > Pepe 9 > Jose 7 > Manuel 6 > > I?m trying to use something like sapply or apply but I can find the > key... > > Can anyone help or guide me ? > > Thanks in advance??? > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at 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.David Winsemius, MD Heritage Laboratories West Hartford, CT