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
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 Fr
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 2
3 matches
Mail list logo