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]]
______________________________________________
[email protected] 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.