ðRèšèªã䜿ã£ãŠã¿ããïŒ â¡ç®è¡æŒç®ã®ããæ¹ãšå€æ°
ãã¯ããããããŸãã çŽ åŽç²å€®ã§ãã
趣å³ã§æ°æ€1çŽã®åŠç¿ããçµ±èšåŠã®åŠç¿ã«åãçµãã§ããŸãã
ä»åããŠããææ¥ã§ã¯ãããããæšæž¬çµ±èšã®åéã«å ¥ããŸãããäžæ¹ã§ä»å¹Žã«å ¥ã£ãŠããã¯çµ±èšåéã§ãã䜿ãããããã°ã©ãã³ã°èšèªãRãã«ã€ããŠã®åŠç¿ãé²ããŠããŠããã¡ãã®åŠç¿ã楜ãããªã£ãŠãããšããã§ãã
ãã1é±éã®åŠç¿èšé²ã¯ãã¡ãã
ããããŒç»åã¯Dream by Womboã§å¶äœããŠããŸãã
StyleïŒFestive v3
Rèšèªã䜿ã£ãŠã¿ããïŒ
å é±ã®èšäºã§ãRèšèªïŒRStudioã§ã®ããã°ã©ãã³ã°ç°å¢ã®æ§ç¯ãè¡ããŸããã
ãããããRStudioã䜿ã£ãRèšèªã§ã®ããã°ã©ãã³ã°ã®ããæ¹ããåºç€ãããã£ãããšåŠãã§ãããããšæããŸãã
ç®æšã¯ãRèšèªã®åºç€ãç解ããŠãåºæ¬çãªçµ±èšåŠçãæšå®ã»æ€å®ãååž°åæãã§ããããã«ãªãããšãããšããã§ãã
ä»åã®èšäºã§ã¯ãå€æ°ã®åºæ¬çãªäœ¿ãæ¹ãšæ°å€ã®ç®è¡æŒç®ã®ããæ¹ãåŠã³ãååæŒç®ãªã©ã®ç°¡åãªèšç®ã®ããæ¹ãèŠãŠãããŸãã
RèšèªïŒRStudioã®åŠç¿ã«ã¯ãäžèšã®æžç±ã䜿ã£ãŠãããŸãã
Rèšèªã«ããããå€ããšã¯ïŒ
ããã°ã©ãã³ã°ã§ã¯ããå€ããšåŒã°ããããŒã¿ãæäœããŠåŠçãçµã¿ç«ãŠãŠãããŸããå€ã«ã¯ããã€ãçš®é¡ããããRèšèªã«ãããŠã¯å€§ããåãããšããã£ãã次ã®3ã€ã®çš®é¡ããããŸãã
æ°å€åïŒããã«æŽæ°ã»å®æ°ã»è€çŽ æ°ã«åãããïŒ
æååå
çåœå€å
ãããå€ã®çš®é¡ã®ããšããåããšåŒã³ãŸããã詳ããããšã¯ä»åŸã®èšäºã§è§ŠããŸããããŸãæåã¯åã®éããæ°ã«ããã«ãRèšèªã§ã§ããæ°å€ã®ç®è¡æŒç®ã«ã€ããŠèŠãŠãããããšæããŸãã
å€æ°ãšä»£å ¥
ãå€æ°ããšã¯ãå€ãä¿ç®¡ããç®±ã®ãããªãã®ã§ããããã°ã©ãã³ã°ã§ã¯å€æ°ã«äœããã®ååãã€ããŠãããã«å€ãä¿ç®¡ïŒä»£å ¥ïŒããŠæ±ãããšãå€ãã§ãã
ãä»£å ¥ããšã¯ãå€æ°ã«å€ãä¿ç®¡ããæäœã®ããšã§ããä»£å ¥ã«ã¯ãä»£å ¥æŒç®åããšãããã®ã䜿ã£ãŠè¡ããŸãã
å®éã«ãå€æ°ãžå€ãä»£å ¥ããæ¹æ³ãèŠãŠã¿ãŸããããRStudioã®å·Šäžé åã®ã¹ã¯ãªãããšãã£ã¿ã«ä»¥äžã®ããã«å ¥åããŠãããããã°ã©ã ãå®è¡ããŠã¿ãŠãã ããã
a <- 2
num1 <- 3
num2 <- -5
ããã°ã©ã ã®å®è¡ã¯ããCtrl/Cmd + aãã§å šç¯å²ãéžæããŠãããCtrl/Cmd + EnterããæŒãã°ããã®ã§ããã
ãããšãå·Šäžé åã®ã³ã³ãœãŒã«ã«ã以äžã®ããã«å®è¡çµæãåºåãããŸãã
å³äžé åã®ãEnvironmentããèŠãŠã¿ãŸãããã以äžã®ããã«ãå€æ°åãšããã«æ ŒçŽãããŠããå€ã®äžèŠ§ã確èªããããšãã§ããŸãã
ä»£å ¥æŒç®å
ãæŒç®åããšã¯ãå€ã®æŒç®ã®ããã«äœ¿ãèšå·ã®ç·ç§°ã§ããå€æ°ãžã®å€ã®ä»£å ¥ã«ã¯ãä»£å ¥æŒç®åããšãããã®ã䜿ããŸãã
åºæ¬çã«ã¯ãå ã»ã©çšããã<-ããšããèšå·ã䜿ããŸããå€æ°ãšå€ã®é åºãå ¥ãæ¿ããŠãã->ãã䜿ã£ãŠããåãçµæã«ãªããŸãã
# ã©ã¡ãã§ãåã ãå€æ°aã«2ãšããå€ã代å
¥ãããæäœ
a <- 2
2 -> a
ä»ã®ããã°ã©ãã³ã°èšèªã§ã¯ãä»£å ¥æŒç®åãšããŠã=ãããã䜿ãããŸãããRèšèªã§ãäžå¿äœ¿ããŸãããããŸãæšå¥šãããŠããªãæžãæ¹ã®ããã§ãã
å€æ°åã¯ã©ããªã®ãã€ããã°ããã®ïŒ
ããã»ã©ãå€æ°åãšããŠãaããnum1ããnum2ããšãããã®ã䜿ããŸãããã§ã¯ããããã¯ã©ã®ããã«æ±ºããã°ããã§ããããã
å€æ°åã«ã¯æåãæ°åããããïŒ.ïŒãã¢ã³ããŒã¹ã³ã¢ïŒ_ïŒãçµã¿åãããŠã奜ããªååãã€ããããšãã§ããŸãããã ãã以äžã®ãããªä»æ§äžã®ã«ãŒã«ããããŸãã
å€æ°åã®æåã¯æåããããããå§ããïŒæ°åãã¢ã³ããŒã¹ã³ã¢ã¯äœ¿ããªãïŒ
Rèšèªã®äºçŽèªã¯äœ¿ããªãïŒTRUE, FALSE, NULLãªã©ã¯äœ¿ããªãïŒ
äºçŽåŸãäœãªã®ãã¯ãã®ã¡ã®ã¡èŠããŠããã°ããã§ãããã
ãŸããå³æ Œãªã«ãŒã«ãšããããã§ã¯ãªãã®ã§ãããããïŒäººéã«ãšã£ãŠïŒèªã¿ãããã³ãŒããæžãããã®ãåœåèŠåããšãããã®ããããŸããããã«ãããšã以äžã®ããã«å€æ°åãã€ãããšè¯ãããã§ãã
å€æ°åã«ã¯å°æåã®è±åèªã䜿ã
è±åèªãçµã¿åãããéã¯ã¢ã³ããŒã¹ã³ã¢ã䜿ã
ã§ããã ãæå³ãããããããååãã€ãã
ã²ãšãŸãã次ã®ãããªå€æ°åã䜿ãããã ãªãŒãšæããŠããã°è¯ãããã§ãã
a
x
num1
num2
prime_number
prime.number
æ°å€ã®ç®è¡æŒç®
ã§ã¯æ¬¡ã«ãæ°å€å士ã®èšç®ã®ããæ¹ãèŠãŠãããŸããããé»åã§ã§ãããããªç°¡åãªç®è¡æŒç®ãè©ŠããŠã¿ãŸãã
RStudioã®ã¹ã¯ãªãããšãã£ã¿ã«ã以äžã®ããã«èšè¿°ããŠå®è¡ããŠã¿ãŸããããå ã»ã©ã®ç¶ãã§ããæžãããã®ãåé€ããŠããã§ããæ°ãã«ãã¡ã€ã«ãäœæããŠãããŸããŸããã
2 + 5
2 - 5
2 * 5
2 / 5
ããã°ã©ã ãå®è¡ããŠã¿ããšã以äžã®ããã«ååæŒç®ã®çµæãåºåãããŸãã
ã§ããããã ãã ãšãããã°ã©ã ã§ããæå³ã¯å šãæããããŸãããããããã§ãã®ã¡ã®ã¡Rèšèªãçšããããã°ã©ãã³ã°ã§æŽ»çšããããšã«ãªãã®ããå€æ°ã䜿ã£ãæŒç®ã§ãã
a <- 2
b <- 5
a + b
a - b
a * b
a / b
次ã®ããã«ãå€æ°ã«å€ãä»£å ¥ããéã«ç®è¡æŒç®ãçšããåŒã䜿ãããšãå¯èœã§ãã
a <- 2
b <- a + 3
a
b
ç®è¡æŒç®å
ç®è¡æŒç®ã®éã«çšããã+ããâããšãã£ãèšå·ã¯ãç®è¡æŒç®åããšåŒã°ãããã®ã§ããRèšèªã®ç®è¡æŒç®åã«ã¯ãååæŒç®ã®ä»ã«ãããã€ããããŸãã以äžã«è¡šãšããŠãŸãšããŠãããŸãã
é€ç®ãšå°äœãæŽæ°é€ç®
äžèšã®ç®è¡æŒç®åã®äžã§ããå°äœããšãæŽæ°é€ç®ããšãããã®ã¯ãªãã¿ãå°ãªããããããŸãããããããã«ã€ããŠç°¡åã«èŠãŠãããŸãããã
以äžã®ããã«ã10ãš3ã®é€ç®ã»æŽæ°é€ç®ã»å°äœãã®çµæãå®è¡ããŠãã©ãããå€ãåºåãããããèŠæ¯ã¹ãŠã¿ãŸãããã
10 / 3 #é€ç®ããããâã3.333333
10 %/% 3 #æŽæ°é€ç®ããâã3
10 %% 3 #å°äœããããâã1
ããããã以äžã®ãããªæŒç®çµæãåŸãããŸãã
é€ç®ã¯ãå²ãç®ããçµæãå®æ°å€ã§åŸãã
æŽæ°é€ç®ã¯ãå²ãç®ããçµæã®æŽæ°éšåã®å€ãæŽæ°å€ã§åŸã
å°äœã¯ãå²ãç®ããçµæã®äœãã®å€ãæŽæ°å€ã§åŸã
ç®è¡æŒç®ã®åªå é äœ
å ã«èšç®ãããåŒã«ã¯ãæ®æ®µæ°åŒãæ±ãå ŽåãšåãæèŠã§ãäžžæ¬åŒ§ã䜿ãããšãã§ããŸãã
3 * (2 + 5)
ãŸããç®è¡æŒç®åã«ã¯åªå é äœãããã以äžã®ãããªé åºãšãªããŸããå°äœã»æŽæ°é€ç®ãé€ãã°ãæ®æ®µæ°åŒãæ±ãå Žåãšåãã§ããã
äžžæ¬åŒ§
ã¹ãç®
å°äœãæŽæ°é€ç®
ä¹ç®ãé€ç®
å ç®ãæžç®
è£è¶³ïŒããã°ã©ã ã®æžãæ¹ã«ã€ããŠ
ããã°ã©ã äžã®åè§ã¹ããŒã¹ã¯ç¡èŠããã
ããã€ããæ°å€ã®æŒç®ä»¥å€ã®è©±é¡ããŸãšããŠãããŸãããŸããåºæ¬çã«ã¯ããã°ã©ã äžã®åè§ã¹ããŒã¹ã¯ç¡èŠãããŸããããšãã°ã³ãŒããèŠãããããç®çã§ãæŒç®ã®ååŸã«ã¹ããŒã¹ã空ããŠèšè¿°ããããšããããŸãã
# 以äžã®2ã€ã¯åãçµæ
a<-2
a <- 2
# 以äžã®2ã€ãåãçµæ
a+5
a + 5
ã³ã¡ã³ããèšè¿°ãã
Rèšèªã®ããã°ã©ã ã®äžã«ãã³ã¡ã³ããã#ãèšå·ã䜿ã£ãŠæžãããšãã§ããŸããã³ã¡ã³ãã¢ãŠããããå 容ã¯å®è¡çµæã«ã¯é¢ä¿ãªãã®ã§ãã¡ã¢æžããä»ã®äººã«äŒãããå 容ãèšããŠããããšãã§ããŸãã
# äžè¡ãã³ã¡ã³ãã¢ãŠãããã
a <- 2
b <- a + 3 # 以éãã³ã¡ã³ãã¢ãŠãããã
ãŸãšã
ä»åã¯ãRèšèªã®å€ãšå€æ°ãæ°å€ã®ä»£å ¥ãæ°å€ã®ç®è¡æŒç®ã«ã€ããŠäž»ã«èŠãŠããŸããã
次åã¯ãæ°åŠé¢æ°ã䜿ã£ãç¹æ®ãªæŒç®ã«ã€ããŠåŠãã§ã¿ãããšæããŸãã