I have a table in R like this:
x Y
1 2 1
2 1 1
3 NA 1
4 2 NA
5 1 2
6 2 2
7 1 1
and what I'm hoping to do is make a new column called xy which bases on if there is a 1 exist in either x or y.
For example, if x is 1 and y is 2 then the xy should be 1 ; if x is NAand y is 1 then the xyshould be 1. If both x and y is 2 then xyshould be 2.
The priority of the categorical variables 1, 2 and NA is 1>2>NA.
In short what my desired output looks like this:
x Y XY
1 2 1 1
2 1 1 1
3 NA 1 1
4 2 NA 2
5 NA NA NA
6 2 2 2
7 1 1 1
I'm new to R and trying to trim my data. Thank you for your help! I'm really appreciated:)