作者paranoia5201 (社會黑暗、行路難)
看板R_Language
標題[問題] 在R裡面的if then output寫法?
時間Mon Jan 8 14:45:35 2018
[問題類型]:
程式諮詢(我想用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
[關鍵字]:
初學者經驗?
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 210.241.78.121
※ 文章網址: https://webptt.com/m.aspx?n=bbs/R_Language/M.1515393938.A.B60.html
1F:→ clansoda: 如果他是文字的話 print(m0) 寫在function裡面的話 01/08 14:52
2F:→ clansoda: return(m0) 01/08 14:52
3F:→ clansoda: if (sex == "0") {print("m0")} 01/08 14:53
4F:→ clansoda: 我後來看懂你的m0不是一個變數了,那你要加""來當文字 01/08 14:54
5F:→ clansoda: 我SAS不是很熟,如果錯了的話請多見諒 01/08 14:54
6F:→ JP1129: m0在SAS裡是資料集的意思 在R裡應該就是data frame的意思 01/08 15:14
7F:推 andrew43: 大致類比成把不同條件資料篩選出來放到不同資料框之中 01/08 15:17
8F:推 andrew43: 。 01/08 15:17
9F:→ clansoda: 哈哈 虧我還考過檢定,那我寫的完全是錯的 不要理我 01/08 15:34
10F:推 alias: m0<- mor[mor[,"sex"]==0,c("year","sex","areaname_new", 01/16 08:28
11F:→ alias: "ADJ_R","TotalCase")] 01/16 08:29
12F:→ alias: 請依需要再微調 01/16 08:31