I have a following matrix and wish to define a variable based the variable A=matrix(0,5,5) A[1,]=c(30,20,100,120,90) A[2,]=c(40,30,20,50,100) A[3,]=c(50,50,40,30,30) A[4,]=c(30,20,40,50,50) A[5,]=c(30,50,NA,NA,100)> A[,1] [,2] [,3] [,4] [,5] [1,] 30 20 100 120 90 [2,] 40 30 20 50 100 [3,] 50 50 40 30 30 [4,] 30 20 40 50 50 [5,] 30 50 NA NA 100 I want to define two variables: X is the first column in each row that is equal to 20, for example, for the first row, I need X=2; 2nd row, X=3; 3rd row, X=NA; 4th row, X=2, 5th row, X=NA; Y is then the first column in each row that is equal to 100 if before this a 20 has been reached, for example, for the first row, Y=3; 2nd row, Y=5; 3rd row, Y=NA, 4th row, Y=NA; 5th row, Y=NA. the matrix may involve NA as well. How can I define these two variables quickly? [[alternative HTML version deleted]]
There may well be more efficient ways to do this, but here's one attempt: foo <- function(x, val) if(any(x == val, na.rm = TRUE)) which(x == val) else NA u <- apply(A, 1, function(x) foo(x, 20L)) v <- apply(A, 1, function(x) foo(x, 100L)) ifelse(u < v, v, NA) [1] 3 5 NA NA NA HTH, Dennis On Fri, Aug 12, 2011 at 7:18 PM, gallon li <gallon.li at gmail.com> wrote:> I have a following matrix and wish to define a variable based the variable > > ?A=matrix(0,5,5) > A[1,]=c(30,20,100,120,90) > A[2,]=c(40,30,20,50,100) > A[3,]=c(50,50,40,30,30) > A[4,]=c(30,20,40,50,50) > A[5,]=c(30,50,NA,NA,100) >> A > ? ? [,1] [,2] [,3] [,4] [,5] > [1,] ? 30 ? 20 ?100 ?120 ? 90 > [2,] ? 40 ? 30 ? 20 ? 50 ?100 > [3,] ? 50 ? 50 ? 40 ? 30 ? 30 > [4,] ? 30 ? 20 ? 40 ? 50 ? 50 > [5,] ? 30 ? 50 ? NA ? NA ?100 > > I want to define two variables: > > X is the first column in each row that is equal to 20, for example, for the > first row, I need X=2; 2nd row, X=3; 3rd row, X=NA; 4th row, X=2, 5th row, > X=NA; > > Y is then the first column in each row that is equal to 100 if before this a > 20 has been reached, for example, for the first row, Y=3; 2nd row, Y=5; 3rd > row, Y=NA, 4th row, Y=NA; 5th row, Y=NA. > > the matrix may involve NA as well. > > How can I define these two variables quickly? > > ? ? ? ?[[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. >
On Aug 12, 2011, at 7:18 PM, gallon li wrote:> I have a following matrix and wish to define a variable based the > variable > > A=matrix(0,5,5) > A[1,]=c(30,20,100,120,90) > A[2,]=c(40,30,20,50,100) > A[3,]=c(50,50,40,30,30) > A[4,]=c(30,20,40,50,50) > A[5,]=c(30,50,NA,NA,100) >> A > [,1] [,2] [,3] [,4] [,5] > [1,] 30 20 100 120 90 > [2,] 40 30 20 50 100 > [3,] 50 50 40 30 30 > [4,] 30 20 40 50 50 > [5,] 30 50 NA NA 100 > > I want to define two variables: > > X is the first column in each row that is equal to 20, for example, > for the > first row, I need X=2; 2nd row, X=3; 3rd row, X=NA; 4th row, X=2, > 5th row, > X=NA;X <- apply(A, 1, function(x) which(x==20) ) is.na(X) <- !unlist(lapply(X, length)) X The first command seems obvious, but the second might be a bit obscure. It says assign NA to any X whose length is non-zero (i.e. positive in the case of length).> > Y is then the first column in each row that is equal to 100 if > before this a > 20 has been reached, for example, for the first row, Y=3; 2nd row, > Y=5; 3rd > row, Y=NA, 4th row, Y=NA; 5th row, Y=NA.Y <- apply(A, 1, function(x) which(x==20)*(which(x==20) < which(x==100) ) ) is.na(Y) <- !unlist(lapply(Y, length)) Y -- David.> > the matrix may involve NA as well. > > How can I define these two variables quickly? > > [[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.David Winsemius, MD Heritage Laboratories West Hartford, CT