注意mutate框里的这句代码 很有趣 可以在循环中应用。
dat_sd=matrix(sample(1:100,20),ncol = 4)
df=dat_sd %>% as.data.frame()
names(df)=paste0(letters[1:4],'aaa')
df1=data.frame()
for (i in 1:4){
i=1
print(i)
a <- median(df[,i], na.rm = TRUE)
ccc=names(df)[i]
df=df %>%
mutate("{ccc}" :=case_when(.[,i]<= a ~'0',
.[,i]>a ~ "1",
TRUE ~ NA_character_))
}