I am looking for an efficient way to solve the following problem. I have a large matrix of continuous values with a small proportion of missing values. Columns correspond to variables where each variable has two measurements, call them A and B. The matrix is such that the columns are in sequence with respect to the variables. I would like to sum up the two measurements for each variable and each observation (the rows) for the whole matrix. Here is an example of such a matrix with 5 variables (R, S, T, U, V) and 50 observations: set.seed(27) Mat <- matrix(rnorm(500), nrow=50, ncol=10) dimnames(Mat)[[2]] <- c("R.A","R.B","S.A","S.B","T.A","T.B","U.A","U.B","V.A","V.B") miss.ind <- rbinom(500,1,prob=0.98) Mat[!as.logical(miss.ind)] <- NA So I would like to produce 50 by 5 matrix where the columns are the sums of A+B measurements for each of the 5 variables. Thanks for any help. Gregory Gentlemen --------------------------------- [[alternative HTML version deleted]]
Hi Gregory, Try this: cs=substr(colnames(Mat),1,1) res=t(apply(t(Mat),2,tapply,cs,sum,na.rm=TRUE)) dim(res) [1] 50 5 HTH, Jorge On Wed, May 21, 2008 at 1:22 PM, Gregory Gentlemen < gregory_gentlemen@yahoo.ca> wrote:> I am looking for an efficient way to solve the following problem. I have a > large matrix of continuous values with a small proportion of missing values. > Columns correspond to variables where each variable has two measurements, > call them A and B. The matrix is such that the columns are in sequence with > respect to the variables. I would like to sum up the two measurements for > each variable and each observation (the rows) for the whole matrix. Here is > an example of such a matrix with 5 variables (R, S, T, U, V) and 50 > observations: > > set.seed(27) > Mat <- matrix(rnorm(500), nrow=50, ncol=10) > dimnames(Mat)[[2]] <- > c("R.A","R.B","S.A","S.B","T.A","T.B","U.A","U.B","V.A","V.B") > > miss.ind <- rbinom(500,1,prob=0.98) > Mat[!as.logical(miss.ind)] <- NA > > So I would like to produce 50 by 5 matrix where the columns are the sums of > A+B measurements for each of the 5 variables. > > Thanks for any help. > > Gregory Gentlemen > > > --------------------------------- > > [[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]]
Try this: t(rowsum(t(Mat), substr(colnames(Mat), 1, 1))) On Wed, May 21, 2008 at 2:22 PM, Gregory Gentlemen < gregory_gentlemen@yahoo.ca> wrote:> I am looking for an efficient way to solve the following problem. I have a > large matrix of continuous values with a small proportion of missing values. > Columns correspond to variables where each variable has two measurements, > call them A and B. The matrix is such that the columns are in sequence with > respect to the variables. I would like to sum up the two measurements for > each variable and each observation (the rows) for the whole matrix. Here is > an example of such a matrix with 5 variables (R, S, T, U, V) and 50 > observations: > > set.seed(27) > Mat <- matrix(rnorm(500), nrow=50, ncol=10) > dimnames(Mat)[[2]] <- > c("R.A","R.B","S.A","S.B","T.A","T.B","U.A","U.B","V.A","V.B") > > miss.ind <- rbinom(500,1,prob=0.98) > Mat[!as.logical(miss.ind)] <- NA > > So I would like to produce 50 by 5 matrix where the columns are the sums of > A+B measurements for each of the 5 variables. > > Thanks for any help. > > Gregory Gentlemen > > > --------------------------------- > > [[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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]