Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何为“编写等效的R代码?”;选择Sas中的语句";_R_Sas - Fatal编程技术网

如何为“编写等效的R代码?”;选择Sas中的语句";

如何为“编写等效的R代码?”;选择Sas中的语句";,r,sas,R,Sas,我正在SAS中编写代码,现在需要将其转换为R 我已经粘贴了我的r代码,但我知道这是不正确的。我需要使用类似“在SAS中选择”的内容。请帮我 在R中转换此sas代码 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; * i

我正在SAS中编写代码,现在需要将其转换为R 我已经粘贴了我的r代码,但我知道这是不正确的。我需要使用类似“在SAS中选择”的内容。请帮我 在R中转换此sas代码

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;

您可以使用
dplyr
我没有在R中翻译by语句,因为它在代码后面没有任何影响。 基本上,一切都可以变成
mutate
函数,当
替换
select
SAS代码时,
case\u

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)
                   )
库(dplyr)
t2%突变(R_G_Right=G1_Right,
M_G_Right=G1_Right_黄斑病变,
R_G_左=G1_左,
M_G_Left=G1_Left_黄斑病变,
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_黄斑病变),G2_Left_黄斑病变,M_G_Left),
重新分级器=情况(
is.na(R_G_Right)| R_G_Right==0~0,
%c(1,2)~1中的R_G_右%,
R_G_Right==3~2,
对(第3页)
)

查看
case\u当
在SAS代码中设置后,为什么要使用
by