I am writing a code in SAS and now I need to convert it into R i have pasted my r code, but i know this is not correct. I need to use something like "Select in SAS". Please help me to convert this sas code in R
data t2; set t; by nhi ass_yr ass_mth;
R_G_Right = G1_Right;
M_G_Right = G1_Right_Maculopathy;
R_G_Left = G1_Left;
M_G_Left = G1_Left_Maculopathy;
* if G2 performed, use this ;
if G2_Right ne . then R_G_Right = G2_Right;
if G2_Right_Maculopathy ne . then M_G_Right = G2_Right_Maculopathy;
if G2_Left ne . then R_G_Left = G2_Left;
if G2_Left_Maculopathy ne . then M_G_Left = G2_Left_Maculopathy;
* collapse grades ;
* 0 - remains 0 ;
* 1-2 -> 1 ;
* 3 -> 2 ;
* 4-5-6 -> 3 ;
select (R_G_Right);
when (.) retgradeR = 0;
when (0) retgradeR = 0;``
when (1) retgradeR = 1;
when (2) retgradeR = 1;`enter code here`
when (3) retgradeR = 2;
otherwise retgradeR = 3;
end;
You can do it this way using dplyr
I did not translate the by statement in R because it does not have any impact later in the code.Basically everything can be turned into a mutate
function, and case_when
replaces the select
SAS code.
library(dplyr)
t2 <- t %>% mutate(R_G_Right = G1_Right,
M_G_Right = G1_Right_Maculopathy,
R_G_Left = G1_Left,
M_G_Left = G1_Left_Maculopathy,
R_G_Right = ifelse(!is.na(G2_Right),G2_Right,R_G_Right),
M_G_Right = ifelse(!is.na(G2_Right_Maculopathy),G2_Right_Maculopathy,M_G_Right),
R_G_Left = ifelse(!is.na(G2_Left),G2_Left,R_G_Left),
M_G_Left = ifelse(!is.na(G2_Left_Maculopathy),G2_Left_Maculopathy,M_G_Left),
retgradeR = case_when(
is.na(R_G_Right)| R_G_Right==0 ~ 0,
R_G_Right %in% c(1,2) ~ 1,
R_G_Right==3 ~2,
TRUE ~ 3)
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.