Re: [R] Matrix subscripting to wrap around from end to start of row

2010-10-14 Thread Greg Snow
> -Original Message- > From: r-help-boun...@r-project.org [mailto:r-help-boun...@r- > project.org] On Behalf Of Alisa Wade > Sent: Wednesday, October 13, 2010 2:11 PM > To: r-help@r-project.org > Subject: [R] Matrix subscripting to wrap around from end to start of > row > >

Re: [R] Matrix subscripting to wrap around from end to start of row

2010-10-13 Thread Dennis Murphy
Hi: This isn't particularly elegant, but I think it works: # The function to be applied: f <- function(x, idx) { n <- length(x) if(idx[1] < idx[2]) {idx <- seq(idx[1], idx[2]) } else { idx <- c(seq(idx[1], n), seq(1, idx[2])) } mean(x[idx]) } # tests > month.data = matrix(c(3,

Re: [R] Matrix subscripting to wrap around from end to start of row

2010-10-13 Thread David Winsemius
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 woul

Re: [R] Matrix subscripting to wrap around from end to start of row

2010-10-13 Thread Alisa Wade
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 start=4; end=2; year.avg = apply(month.data[, c(start,end)],

Re: [R] Matrix subscripting to wrap around from end to start of row

2010-10-13 Thread David Winsemius
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

[R] Matrix subscripting to wrap around from end to start of row

2010-10-13 Thread Alisa Wade
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