[R] Average value in a particular range of a matrix

arun smartpink111 at yahoo.com
Thu Jun 12 14:29:59 CEST 2014


Hi,
Try:

set.seed(45)
mat1 <- matrix(rnorm(500), ncol=1)
 n <- dim(mat1)[1]
indx <- as.numeric(gl(n,100,n))
 tapply(mat1[,1], indx, FUN= mean, na.rm=TRUE)
#          1           2           3           4           5 
# 0.08118335 -0.05222418 -0.20042223 -0.03530833 -0.06093053 

If you have more than one column:
set.seed(49)
 dat1 <- as.data.frame(matrix(rnorm(1000), ncol=2))
library(plyr)
ddply(dat1,.(indx), numcolwise(mean, na.rm=TRUE))

A.K.





On Thursday, June 12, 2014 8:11 AM, Pat-74100 <leonardsquall15 at hotmail.com> wrote:




Hi

@Rolf Turner: So you're wrong, I can guaranty you this is not homework ... but just for a graph I'm trying do plot.

@Arun: Yes I've done a mistake, it is 1-100 , 101-200 etc .. or even 1-50,51-100 etc. The range is not important.

Thanks



> Date: Thu, 12 Jun 2014 02:04:26 -0700
> From: smartpink111 at yahoo.com
> Subject: Re: [R] Average value in a particular range of a matrix
> To: leonardsquall15 at hotmail.com
> 
> Hi Pat,
> Is it 1-100, 101-200, 201-300,.. or just the way you described?
> A.K.
> 
> 
> 
> 
> On Wednesday, June 11, 2014 11:45 PM, Pat-74100 <leonardsquall15 at hotmail.com> wrote:
> Hi
> 
> I have a matrix with values of size 1*500  i have to find the avg 
> value of first 1 to 100 ,avg value of 100 - 200 and so on up to 400-500.
> is there any function to find the average of 1 -100 and 100 - 200, 200 -
> 300,300 - 400,400 - 500 ??
> 
> Thanks
>                           
>     [[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.
>



More information about the R-help mailing list