Skip to content

Calculateing means

7 messages · Khan, Sohail, John Kane, ilai +2 more

#
?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
____________________________________________________________
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
#
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
____________________________________________________________
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 privilege.
#
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
____________________________________________________________
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.
#
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
____________________________________________________________
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.
2 days later
#
Write a function that calculates the sample means for a vector of 95 observations and then use apply() to apply that function to the whole matrix.

S

*******************************************************************
This email and any attachments are confidential. Any use...{{dropped:8}}