I have a dataset with following columns :
ID Measure1 Measure2 XO X1 x2 x3 x4 x5
1 30 2 item1 item1 item23 NA item6 item9
2 23 2 item1 item323 item1 item4 item5 NA
3 2 2 item1 item78 item3 NA item1 item5
and I want to create a flag variable with this this piece of SAS Code in R:
data dt2;
set dt1;
array x {5} x1 - x5;
do i=1 to 5;
if x0=x{i} then do;
flag=i;
leave;
end;
end;
drop i;
run;
The goal is to be able to browse thorugh the values of x1-x5 and see where xo is equal to any of them and return the position , for example if item1 is found at x1 then return to me value 1 if found in position x3 return 3.
The end product would look something like this:
ID Measure1 Measure2 XO X1 x2 x3 x4 x5 Flag
1 30 2 item1 item1 item23 NA item6 item9 1
2 23 2 item1 item323 item1 item4 item5 NA 2
3 2 2 item1 item78 item3 NA item1 item5 4
Keep in mind that there might be cases where all rows rom x1-x5 contain NA, in that case i would like to return blank, is this possible?
I haven’t been able to find in R something equivalent in the sense of being dynamic (without writing multiple if statements or case when with sqldf) because now the columns might be 5 but can alter in the future to up to 20.
Any ideas?