Alisa Wade
2010-Oct-13 20:10 UTC
[R] Matrix subscripting to wrap around from end to start of row
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) 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 National Center for Ecological Analysis and Synthesis wade@nceas.ucsb.edu (406) 529-9722 home email: alisaww@gmail.com [[alternative HTML version deleted]]
David Winsemius
2010-Oct-13 20:23 UTC
[R] Matrix subscripting to wrap around from end to start of row
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 AssociateDavid Winsemius, MD West Hartford, CT
Dennis Murphy
2010-Oct-14 00:12 UTC
[R] Matrix subscripting to wrap around from end to start of row
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,4,6,8,12,90,5,14,22, 8), nrow = 2, ncol=5) > month.data[,1] [,2] [,3] [,4] [,5] [1,] 3 6 12 5 22 [2,] 4 8 90 14 8> apply(month.data, 1, f, idx = c(1, 3))[1] 7 34> apply(month.data, 1, f, idx = c(3, 1))[1] 10.5 29.0> apply(month.data, 1, f, idx = c(5, 2))[1] 10.333333 6.666667 HTH, Dennis On Wed, Oct 13, 2010 at 1:10 PM, Alisa Wade <alisaww@gmail.com> 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) > 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 > National Center for Ecological Analysis and Synthesis > wade@nceas.ucsb.edu > (406) 529-9722 > home email: alisaww@gmail.com > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >[[alternative HTML version deleted]]
Greg Snow
2010-Oct-14 16:26 UTC
[R] Matrix subscripting to wrap around from end to start of row
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 %/% operators to find the vector indexes of the elements and just access them directly rather than as matrix elements (a matrix is just a vector that knows its dimensions). -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at imail.org 801.408.8111> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of Alisa Wade > Sent: Wednesday, October 13, 2010 2:11 PM > To: r-help at r-project.org > Subject: [R] Matrix subscripting to wrap around from end to start of > row > > 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) > 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 > National Center for Ecological Analysis and Synthesis > wade at nceas.ucsb.edu > (406) 529-9722 > home email: alisaww at gmail.com > > [[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.