lundi 26 juillet 2021

Create a new column based on different reference ranges and 2 conditions

I have this dataset:

df <- data.frame(PatientID = c("0002" ,"0002", "0005", "0005" ,"0009" ,"0009" ,"0018", "0018" ,"0020" ,"0027", "0039" ,"0039" ,"0042", "0043" ,"0043" ,"0045", "0046", "0046" ,"0048" ,"0048", "0055"),
                 Timepoint= c("A", "B", "A", "B", "A", "B", "A", "B", "A", "A", "A", "B", "A", "A", "B",  "A",  "A", "B", "A", "B", "A"),
                 AXV= c("1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "1", "2", "2",  "2",  "2", "2", "2", "2", "1"),
                 A = c(NA , 977.146 , NA , 964.315 ,NA , 952.311 , NA , 950.797 , 958.975  ,960.712  ,NA , 947.465 , 902.852 , NA,  985.124  ,NA , 930.141 ,1007.790 , 948.848, 1027.110 , 999.414),
                 B = c(998.988 , NA , 998.680 , NA , NA ,1020.560 , 947.751 ,1029.560 , 955.540 , 911.606 , 964.039   ,    NA,  988.087 , 902.367 , 959.338 ,1029.050 , 925.162 , 987.374 ,1066.400  ,957.512 , 917.597),
                 C = c( NA , 987.140 , 961.810 , 929.466 , 978.166, 1005.820  ,925.752 , 969.469 , 943.398  ,936.034,  965.292 , 996.404 , 920.610 , 967.047  ,986.565 , 913.517 , 893.428 , 921.606 , NA , 929.590  ,950.493), 
                 D = c(975.634 , 987.140 , 961.810 , 929.466 , 978.166, 1005.820 , 925.752 , 969.469  ,943.398 , NA , 965.292 , 996.404 , NA , 967.047 , 986.565 , NA , 893.428 , 921.606 , 976.192 , 929.590 , 950.493),
                 E = c(1006.330, 1028.070 , NA , 954.274 ,1005.910  ,949.969 , 992.820 , 977.048  ,934.407 , 948.913 , NA , NA , NA,  961.375  ,955.296 , 961.128  ,998.119 ,1009.110 , 994.891 ,1000.170  ,982.763),
                 G= c(NA , 958.990 , NA , NA , 924.680 , 955.927 , NA , 949.384  ,973.348 , 984.392 , 943.894 , 961.468 , 995.368 , 994.997 , NA , 979.454 , 952.605 , NA  ,   NA, NA , 956.507), stringsAsFactors = F)

And I need to create a tricky code...

The variables that are of my interest are A, B, C, D, E and G. And I need to create a new dihcotomic variable (yes/no) based on three out of 6 of the following variables: "A, B, C, D, E and G" being out of range.

The problem is that there are different reference ranges for each variable, and depending on the df$AXV value

The ranges are the following:

For A when AXV==1 >900 is abnormal

For A when AXV==2 >906 is abnormal

For B when AXV==1 >999 is abnormal

For B when AXV==2 >997 is abnormal

For C when AXV==1 >929 is abnormal

For C when AXV==2 >959 is abnormal

For D when AXV==1 >899 is abnormal

For D when AXV==2 >900 is abnormal

For E when AXV==1 >1000 is abnormal

For E when AXV==2 >1002 is abnormal

For G when AXV==1 >1100 is abnormal

For G when AXV==2 >1202 is abnormal

Basically, for each row, I would like to create a code that will provide me with a new variable df$injury that will be yes if three or more columns are out of range, otherwise no.

Thanks for your help!!

Aucun commentaire:

Enregistrer un commentaire