On Oct 13, 2010, at 4:54 PM, Alisa Wade wrote:

Thanks, David for the response.

Unfortunately, that only works for the case where I happen to only want the last and first month "wrap". But it doesn't work for any other case, e.g., say I want months start.month = 4, end.month = 2.
Now

would give me just the average of months 4 and 2, when I want the average of months 4, 5, 1, 2.


Then just put in the true start's and end's of the loop/series/ring/ whatever:

start=4; end=2; year.avg = apply(month.data[, c(1:start,end:5)], 1, mean)

If you don't know the length of the full cycle, the has to be some way to derive length(series-object):

start=4; end=2; year.avg = apply(month.data[, c(1:start,end:length(series-object))], 1, mean)

--
David.
I could do an if statement:
start = 4
end = 2
if (end < start ) {
  start.script = paste(start, ":5", sep="")
  end.script = paste("1:", 5-end, sep="")
  final.script = paste("c(",start.script,",",end.script,")", sep="")}
out = apply(month.data[,final.script], 1, mean)

But, alas, final.script is now a string, and it can't be passed that way.

Is there either a way to tell the script that 4:2 does not mean 4,3,2, but it means 4,5,1,2 or is there a way to pass final.script into the subscript in the apply command
or am I missing something else?

Thanks again,
Alisa



On Wed, Oct 13, 2010 at 2:23 PM, David Winsemius <dwinsem...@comcast.net > wrote:

On Oct 13, 2010, at 4:10 PM, Alisa Wade wrote:

Perhaps it is just that I don't even know the correct term to search for, but I can find nothing that explains how to wrap around from the end to a
start of a row in a matrix.

For example, you have a matrix of 2 years of data, where rows are years, and
columns are months.
month.data = matrix(c(3,4,6,8,12,90,5,14,22, 8), nrow = 2, ncol=5)

I would like to take the average of months 5:1 for each year (for row 1 =12.5). However, I am passing the start month (5) and the end month (1) as
variables.

I would like to do something like

year.avg = apply(month.data[, start.month:end.month], MARGIN=1, mean)

But that gives me the average of months 1:5. (for row 1 =9.6)

I know I could use:
apply(month.data[, c(1,5)], 1, mean)

> start=5; end=1; year.avg = apply(month.data[, c(start,end)], 1, mean)
> year.avg
[1] 12.5  6.0


but I don't know how to pass start.month, end.month into that format that because paste or sprintf forces them to strings, which are not accepted in a
subscript.

I have the feeling I am unaware of some obvious trick.
Any ideas would be greatly appreciated!

*****************************************************
Alisa A. Wade
Postdoctoral Center Associate


David Winsemius, MD
West Hartford, CT




--
*****************************************************
Alisa A. Wade
Postdoctoral Center Associate
National Center for Ecological Analysis and Synthesis
w...@nceas.ucsb.edu
(406) 529-9722
home email: alis...@gmail.com

David Winsemius, MD
West Hartford, CT

______________________________________________
R-help@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.

Reply via email to