[R] matching identical row names

John Fox jfox at mcmaster.ca
Sat Apr 15 15:35:46 CEST 2006


Dear Srinivas,

Your data are likely in a data frame rather than a matrix (since the columns
are heterogeneous), and name is a variable, not the row names of the data
frame.

There are several ways to do what you want; one simple way, assuming that
the data are in a data frame named Data, is

 by(Data[,2:5], Data$name, mean)

If you want the result in the form of a matrix, then you could do

 aggregate(Data[,2:5], list(Data$name), mean)

I hope this helps,
 John

--------------------------------
John Fox
Department of Sociology
McMaster University
Hamilton, Ontario
Canada L8S 4M4
905-525-9140x23604
http://socserv.mcmaster.ca/jfox 
-------------------------------- 

> -----Original Message-----
> From: r-help-bounces at stat.math.ethz.ch 
> [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Srinivas Iyyer
> Sent: Friday, April 14, 2006 11:58 PM
> To: r-help at stat.math.ethz.ch
> Subject: [R] matching identical row names
> 
> dear group, 
>  
> i have a sample matrix
> name   v1   v2   v3   v4
> cat   10    11   12   15
> dog   3     12   10   14
> cat   9     12   12   15
> cat   5     12   10   11
> dog   12    113  123  31
> ...
> 
> 
> since cat is repeated 3 times, I want a mean value for it. 
> Like wise for every element of the name column. 
> cat v1 = mean(c(10,9,5))
> cat v3 = mean(c(11,12,13))
> ..etc.
> 
> name v1   v2     v3   v4
> cat  8   11.6   11.3  13.6
> dog  7.5 62.5   66.5  22.5
> 
> could any one help me in solving this mystery. thank you.
> 
> ______________________________________________
> 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




More information about the R-help mailing list