这是要转换的sas代码
data dataset;
if anl01="Y" or anl02="Y" then anl="Y";
if ser="" then do;
if con="Y" or disab="Y" or dth="Y" or hosp="Y" or life="Y" or mie="Y" then ser="Y";
else ser="N";
end;
run;
我已经使用if条件编写了这样的R代码,不确定是否可行,但是在此之前我遇到了关于花括号的错误
dataframe<-(if ( is.na(dataframe$SER) && cong=="Y" | disab=="Y" |
dth=="Y" | hosp=="Y" |
life=="Y" | mie=="Y"){
dataframe$ser="Y"
} else {
dataframe$ser="N"
})
答案 0 :(得分:0)
你在这里交配
library(tidyverse)
set.seed(888)
anl01<-factor(rbinom(n=1000,size=1,prob=0.6),labels=c("Y", "N"))
anl02<-factor(rbinom(n=1000,size=1,prob=0.6),labels=c("Y", "N"))
con<-factor(rbinom(n=1000,size=1,prob=0.6),labels=c("Y", "N"))
disab<-factor(rbinom(n=1000,size=1,prob=0.6),labels=c("Y", "N"))
dth<-factor(rbinom(n=1000,size=1,prob=0.6),labels=c("Y", "N"))
hosp<-factor(rbinom(n=1000,size=1,prob=0.6),labels=c("Y", "N"))
life<-factor(rbinom(n=1000,size=1,prob=0.6),labels=c("Y", "N"))
mie<-factor(rbinom(n=1000,size=1,prob=0.6),labels=c("Y", "N"))
ser<-factor(rbinom(n=1000,size=1,prob=0.6),labels=c("", "N"))
Have<-data.frame(anl01,anl02,con,disab,dth,hosp,mie,ser)
Want <-Have %>%
mutate(anl=ifelse(anl01=='Y'| anl02=='Y', 'Y','N'),
ser=ifelse(ser=='' & (con=='Y' | disab=='Y'| dth=='Y' | hosp=='Y' | life=='Y' | mie=='Y'), 'Y', 'N'))