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

2010-10-14 Thread Greg Snow
You might have better luck reformatting your data as time series, then you can still print it in a matrix like form, but access it serially without worrying about wrapping around rows. Another option would be to transpose the matrix so that months go down the columns, then use the %% and %/% op

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