[R] Calculateing means

arun smartpink111 at yahoo.com
Sat Nov 17 00:25:35 CET 2012


HI,

May be this helps:

dat1<-read.table(text="
Indx    x1    x2      x3      x4    x5      x6      x7      x8      x9
 1      25      30      15      8      12      9      18      21      89
 2      52      35      42      74      65      20      28      32      12
 3      12      35      33      88      12      52      32      32      18
 4      25      25      16      23      89      21      21      21      42
",sep="",header=TRUE)

names(dat1)[2:10]<-paste(names(dat1)[2:10],"_",rep(1:3,each=3),sep="") # in your original dataset, change the number accordingly rep(1:95,each=6)
ids<-1:3
#Jim's solution from the archives
 res1<-NULL
fun1<-function(dat,rep_id){
 for(i in rep_id){
 Indx<-grepl(paste(i,"$",sep=""),names(dat))
 res1<-cbind(res1,rowMeans(dat[,Indx],na.rm=TRUE))
 colnames(res1)[ncol(res1)]<-i}
 res1}


 fun1(dat1,ids)
#            1         2        3
#[1,] 23.33333  9.666667 42.66667
#[2,] 43.00000 53.000000 24.00000
#[3,] 26.66667 50.666667 27.33333
#[4,] 22.00000 44.333333 28.00000



A.K.




----- Original Message -----
From: "Khan, Sohail" <SKhan30 at nshs.edu>
To: "'r-help at r-project.org'" <r-help at r-project.org>
Cc: 
Sent: Friday, November 16, 2012 4:42 PM
Subject: Re: [R] Calculateing means

Thanks. But aggregate will work on rows or columns.  I need to calculate mean for subsets of rows in a matrix
I.E.

Indx     x1     x2     x3     x4    x5     x6     x7     x8     x9
1    25    30    15    8    12    9    18    21    89
2    52    35    42    74    65    20    28    32    12
3    12    35    33    88    12    52    32    32    18
4    25    25    16    23    89    21    21    21    42
........................................
...................................
I would like to calculate means for x1-x3, x4-x6, x7-x9
For each row.

-Sohail


-----Original Message-----
From: John Kane [mailto:jrkrideau at inbox.com] 
Sent: Friday, November 16, 2012 4:35 PM
To: Khan, Sohail; 'r-help at r-project.org'
Subject: RE: [R] Calculateing means

?aggregate will do it.

x <- data.frame( height= c(50, 174, 145, 200, 210, 140, 175), age_group=c(1,2,2,1,1,2,1), 
                     ville= c(1,2,3,1,2,3,1))

aggregate(x$height,list(x$age_group, x$ville), mean)

or have a look at the plyr or datatable packages.

John Kane
Kingston ON Canada


> -----Original Message-----
> From: skhan30 at nshs.edu
> Sent: Fri, 16 Nov 2012 15:58:17 -0500
> To: r-help at r-project.org
> Subject: [R] Calculateing means
> 
> 
> Dear List,
> 
> I have a data matrix with 570 columns containing 95 (samples) with 6 
> replicates each.
> How can I calculate the mean of the replicates for 95 samples?
> Thank you.
> 
> 
> The information contained in this electronic e-mail transmission and 
> any attachments are intended only for the use of the individual or 
> entity to whom or to which it is addressed, and may contain 
> information that is privileged, confidential and exempt from disclosure under applicable law.
> If the reader of this communication is not the intended recipient, or 
> the employee or agent responsible for delivering this communication to 
> the intended recipient, you are hereby notified that any 
> dissemination, distribution, copying or disclosure of this 
> communication and any attachment is strictly prohibited. If you have 
> received this transmission in error, please notify the sender 
> immediately by telephone and electronic mail, and delete the original 
> communication and any attachment from any computer, server or other 
> electronic recording or storage device or medium. Receipt by anyone 
> other than the intended recipient is not a waiver of any attorney-client, physician-patient or other priv!
>  ilege.
>     [[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.

____________________________________________________________
Send any screenshot to your friends in seconds...
Works in all emails, instant messengers, blogs, forums and social networks.
TRY IM TOOLPACK at http://www.imtoolpack.com/default.aspx?rc=if2 for FREE




The information contained in this electronic e-mail transmission and any attachments are intended only for the use of the individual or entity to whom or to which it is addressed, and may contain information that is privileged, confidential and exempt from disclosure under applicable law. If the reader of this communication is not the intended recipient, or the employee or agent responsible for delivering this communication to the intended recipient, you are hereby notified that any dissemination, distribution, copying or disclosure of this communication and any attachment is strictly prohibited. If you have received this transmission in error, please notify the sender immediately by telephone and electronic mail, and delete the original communication and any attachment from any computer, server or other electronic recording or storage device or medium. Receipt by anyone other than the intended recipient is not a waiver of any attorney-client,
 physician-patient or other priv!
ilege.
______________________________________________
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.





More information about the R-help mailing list