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   20   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>5; 3th 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>5; 5th row, Y=NA.

the matrix may involve NA as well.

How can I define these two variables quickly? (When X>5 or Y>5, we can
arbitrarily assign a value 6, and this is different from being NA)

        [[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.

Reply via email to