Re: [问题] 在R里面的if then output写法?

楼主: raydai (Ray)   2018-01-16 02:28:35
※ 引述《paranoia5201 (社会黑暗、行路难)》之铭言:
: [问题类型]:
: 程式咨询(我想用R 做某件事情,但是我不知道要怎么用R 写出来)
: [软件熟悉度]:
: 入门(写过其他程式,只是对语法不熟悉)
: [问题叙述]:
: 以下是一个SAS的简单程式,想问在R中要如何也做出同样的事情。
: 这里是针对if then "output"的部分。
: 若需要先下载某packages也请直说!
: [程式范例]:
: data m0 m1 m2;
: set mor;
: keep year sex areaname_new ADJ_R TotalCase;
: if type = "综合" & areaname_new = "全国" ;
: if sex = "0" then output m0;
: if sex = "1" then output m1;
: if sex = "2" then output m2;
: run;
: [环境叙述]:
: R3.2.3
: [关键字]:
: 初学者经验?
我也很久没有使用SAS,不过对于我的理解,我就想到这样写.或许有更容易的写法
提供给你参考
==================================================================================
## create a fake dataset
mydata<-data.frame("year"=c(1986,2012,2013,2020,2019,1999,1986,2012,2013,2020,2019,1999),
"type"=rep(c("A","B"),6),
"sex"=rep(c("male","female"),6),
"areaname_new"=rep(c("global","local"),6),
"ADJ_R"=rnorm(n =12,mean = 0,sd = 1),
"TotalCase"=rnorm(n = 12,mean = 5,sd = 1.5))
library("dplyr") ## Data manupulation
## Assinge the data to a new varaible "m0"
## %>% this is pipe function from "dplyr" package.
mydata%>%filter(type=="A",areaname_new=="global",sex=="male")%>% ## filter
select("year", "sex" ,"areaname_new" ,"ADJ_R", "TotalCase")%>% ## Keep the variables you want
assign("m0",value =.,envir =.GlobalEnv ) ## Assign the dataset to a new varaible
==========================================================================================

Links booklink

Contact Us: admin [ a t ] ucptt.com