[R] Calculateing means

arun smartpink111 at yahoo.com
Sat Nov 17 02:41:24 CET 2012


Hi,

If the replicated columns are adjacent to each other in the order as shown in the example,
you could also try:
set.seed(25)
mat1<-matrix(sample(1:200,2280,replace=TRUE),ncol=570)
dat1<-data.frame(Indx=1:4,mat1)


#Here, I assume equal replications as mentioned in the post fun1<-function(dat,sample,rep){
names(dat)[-1]<-paste(names(dat)[-1],"_",rep(1:sample,each=rep),sep="")
names(dat)[-1]<-gsub(".*\\_(\\d+)","\\1",names(dat)[-1])
res<-do.call(data.frame,lapply(split(seq_along(dat[,-1]),names(dat)[-1]),function(i) rowMeans(dat[,-1][i])))
res}

fun1(dat1,95,6)
 fun1(dat1,95,6)[,1:4]
#        X1       X10      X11       X12
#1  72.0000  84.16667 120.5000 109.33333
#2 124.8333 132.00000  87.5000 141.83333
#3  79.5000 108.83333 105.8333  78.66667
#4  88.0000  69.50000 120.1667 156.16667

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