Average value in a particular range of a matrix

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Average value in a particular range of a matrix

Pat-74100
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]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Average value in a particular range of a matrix

Rolf Turner
On 12/06/14 00:17, Pat-74100 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 ??

This looks very much like homework.  The r-help list has a "no homework"
policy.  Do your own homework.  That way you might actually learn something!

cheers,

Rolf Turner

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Average value in a particular range of a matrix

Pat-74100
In reply to this post by Pat-74100

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: [hidden email]
> Subject: Re: [R] Average value in a particular range of a matrix
> To: [hidden email]
>
> 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 <[hidden email]> 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]]
>
> ______________________________________________
> [hidden email] 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.
>
     
        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Average value in a particular range of a matrix

arun kirshna
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 <[hidden email]> 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: [hidden email]
> Subject: Re: [R] Average value in a particular range of a matrix
> To: [hidden email]
>
> 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 <[hidden email]> 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]]
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Average value in a particular range of a matrix

John Kane
In reply to this post by Pat-74100
?mean

You need to read tutorial or two, particularly with reference to indexing.

The CRAN site has any number of useful papers and books that you can download.

John Kane
Kingston ON Canada


> -----Original Message-----
> From: [hidden email]
> Sent: Wed, 11 Jun 2014 12:17:02 +0000
> To: [hidden email]
> Subject: [R] Average value in a particular range of a matrix
>
> 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]]
>
> ______________________________________________
> [hidden email] 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.

____________________________________________________________
FREE 3D MARINE AQUARIUM SCREENSAVER - Watch dolphins, sharks & orcas on your desktop!

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Average value in a particular range of a matrix

David Winsemius
In reply to this post by Pat-74100

On Jun 12, 2014, at 5:11 AM, Pat-74100 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.
>

In that case,  something along the lines of :

  sapply( seq(1, maxval, by=50), function (x) mean( some_object[x:(x+49)] ) )

Pretty basic. The advice to read more basic tutorials and play around with the common loop idioms on their help page examples might be worth your effort.

--
david.



> Thanks
>
>
>> Date: Thu, 12 Jun 2014 02:04:26 -0700
>> From: [hidden email]
>> Subject: Re: [R] Average value in a particular range of a matrix
>> To: [hidden email]
>>
>> 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 <[hidden email]> 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


David Winsemius
Alameda, CA, USA

______________________________________________
[hidden email] 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.