![èŠåºãç»å](https://assets.st-note.com/production/uploads/images/128749727/rectangle_large_type_2_463bb5b7d485ed12372189e87613168a.jpeg?width=1200)
ðRèšèªã䜿ã£ãŠã¿ããïŒ â¢æ°åŠé¢æ°ã®äœ¿ãæ¹
ãã¯ããããããŸãã çŽ åŽç²å€®ã§ãã
趣å³ã§æ°æ€1çŽã®åŠç¿ããçµ±èšåŠã®åŠç¿ã«åãçµãã§ããŸãã
ããæè¿ã¯ã©ãã«ãããŸãåŠç¿ã®æéããšãããç¿æ £ãä¹±ããŠããŸããŸãããæ°ãåãçŽããŠããããã§ãããååã«åŒãç¶ããçµ±èšåéã§ãã䜿ãããããã°ã©ãã³ã°èšèªãRãã«ã€ããŠåŠãã§ãããŸãããã
ãã1é±éã®åŠç¿èšé²ã¯ãã¡ãã
ããããŒç»åã¯Dream by Womboã§å¶äœããŠããŸãã
StyleïŒFestive v3
Rèšèªã䜿ã£ãŠã¿ããïŒ
å é±ã®èšäºã§ãRèšèªïŒRStudioã§ã®ç®è¡æŒç®ãå€æ°ã®äœ¿ãæ¹ãåŠã³ãŸããã
ä»åã¯ãRèšèªã§çšæãããŠããé¢æ°ã䜿ã£ãŠãç®è¡æŒç®ã ãã§ãªããããããªèšç®ãè©ŠããŠã¿ãŸãããã
ããšãã°ã«ãŒããäžè§é¢æ°ã®èšç®ãšãã£ãããšãRèšèªã䜿ããšã§ããããã«ãªããŸãã
ããã°ã©ãã³ã°ã«ããããé¢æ°ããšã¯ïŒ
ããã°ã©ãã³ã°ã«ããããé¢æ°ããšã¯ãäžé£ã®åŠçãã²ãšãŸãšãã«ãããã®ã§ããæ°åŠã«ããããé¢æ°ããšã¯å¥ç©ãšèããæ¹ãæ··ä¹±ãå°ãªãã®ããªãšæããŸãã
é¢æ°ã«ã¯ãRèšèªã§ãããããçšæãããŠããé¢æ°ãšãèªåã§ããã°ã©ã ãæžããŠèªäœããé¢æ°ããããŸãã
é¢æ°ãèªäœããããæ¹ã¯ä»åŸã«åããšããŠãããã§ã¯Rèšèªã§çšæãããŠããé¢æ°ã䜿ãæ¹æ³ã«ã€ããŠèŠãŠãããŸãããã
é¢æ°ã®å©çš
Rèšèªã§é¢æ°ã䜿ãåºæ¬æ§æã¯ä»¥äžã®éãã§ãããŸãé¢æ°åãæžããåŸã«ãã«ãã³ã®äžã«åŒæ°ãšãã°ããå€ãæå®ããŸããé¢æ°ã«ãã£ãŠã¯ãåŒæ°ã«è€æ°ã®å€ãæå®ããããšããããŸãã
é¢æ°å(åŒæ°)
é¢æ°å(åŒæ°1, åŒæ°2, ...)
ããã ãã ãšãããã¥ããã®ã§ãå®éã«ããã°ã©ã ãæžããŠåãããŠã¿ãŸãããã
ã³ã³ãœãŒã«ã«ãã°åºåããé¢æ°
ãŸãã¯ãã¹ã¯ãªãããšãã£ã¿ã«ä»¥äžã®ããã«æžããŠå®è¡ããŠã¿ãŠãã ããã
value <- 5
print(value)
cat('valueã¯', value, 'ã§ã')
ãããšãã³ã³ãœãŒã«ã«ä»¥äžã®ããã«è¡šç€ºãããŠããã¯ãã§ãã
![](https://assets.st-note.com/img/1706252679869-lNGYS7Xgwg.png)
ããã§ã¯ãRèšèªã§çšæãããŠãããprinté¢æ°ããšãcaté¢æ°ããšããã®ã䜿ã£ãŠã¿ãŸãããããããprintããcatããšããã®ãé¢æ°åã«ããããããããã®é¢æ°ã®ã«ãã³ã®äžèº«ã«ããå€ãåŒæ°ã«ããããŸãã
ã¡ãªã¿ã«ãprinté¢æ°ãšcaté¢æ°ã¯ã©ã¡ããå€ãã³ã³ãœãŒã«ã«è¡šç€ºããããã®é¢æ°ãšãªããŸããã以äžã®ãããªéãããããŸãã
printé¢æ°ïŒåŒæ°ã®å€ããã®ãŸãŸè¡šç€ºãã
caté¢æ°ïŒè€æ°ã®åŒæ°ããšãããããã®å€ãæååãšããŠã€ãªããŠè¡šç€ºãã
RèšèªãåŠãã§ãããšcaté¢æ°ã¯äŸ¿å©ã§ããåºãŠãããããé ã®çé ã«ã§ãå ¥ããŠãããšè¯ãããã§ãã
ãã¯ãã«ãçæããé¢æ°
次ã«ãã¹ã¯ãªãããšãã£ã¿ã«ä»¥äžã®ããã«æžããŠå®è¡ããŠã¿ãŠãã ããã
vec <- c(2, 4, 6, 8, 10)
vec
ãããšãã³ã³ãœãŒã«ã«ä»¥äžã®ããã«è¡šç€ºãããŠããã¯ãã§ãã
![](https://assets.st-note.com/img/1706252744864-R0TeVARTHE.png)
ãªã«ãã2ãã10ãŸã§ã®å¶æ°ãåºåãããŠããŸããã
ããã§ã¯ããã¯ãã«ããšããè€æ°ã®å€ãäžçµã«ããããŒã¿ãçæãããcé¢æ°ããšãããã®ã䜿ã£ãŠããŸãããcããé¢æ°åã§ãã«ãã³ã®äžã«æžããã5ã€ã®æŽæ°å€ãåŒæ°ã«ãªããŸãã
cé¢æ°ã®è©³ãã䜿ãæ¹ããã¯ãã«ã«ã€ããŠã¯ãŸãå¥ã®æ©äŒã«èŠãŠãããããšæããŸãã®ã§ãããã§ã¯ãcé¢æ°ã䜿ããšãè€æ°ã®å€ããŸãšããŠæ±ãããã¯ãã«ãšããå€ãäœãããªããã ãªãŒããšããã€ã¡ãŒãžã§æããŠãããŠãã ããã
ãã®èšäºã®æåŸã§ããã¯ãã«ã䜿ã£ãç°¡åãªèšç®ãè©ŠããŠã¿ãŸãã
ãããããªæ°åŠé¢æ°
é¢æ°ã®äœ¿ãæ¹ãç°¡åã«èŠããšããã§ãå®éã«é¢æ°ã䜿ã£ãŠæ°å€èšç®ãè¡ã£ãŠã¿ãŸãããã
ãããããªæ°åŠé¢æ°ã®äžèŠ§è¡š
Rèšèªã§ã¯ãæ°åŠã§ãã䜿ãèšç®ãè¡ãé¢æ°ããããããšçšæãããŠããŸããããšãã°ä»¥äžã®ãããªé¢æ°ããããŸãã
![](https://assets.st-note.com/img/1706254406820-eopXeL97ue.png)
䜿ã£ãŠã¿ãã
ãŸããå¹³æ¹æ ¹ãšææ°ã»å¯Ÿæ°ã®èšç®ãèŠãŠã¿ãŸããããã¹ã¯ãªãããšãã£ã¿ã«ä»¥äžã®ããã«æžããŠå®è¡ããŠã¿ãŠãã ããã
sqrt(2)
exp(2)
log(2)
log10(2)
log2(2)
![](https://assets.st-note.com/img/1706253787479-B0xrTMOA79.png)
ããããã$${\sqrt 2}$$, $${e^2}$$, $${log_e 2}$$($${\ln 2}$$), $${\log_{10} 2}$$, $${log_2 2}$$ã®å€ãåºåãããŸãã ã«ãã³ã®åŒæ°ã®å€ãå€ããããšã§ãèªç±ã«èšç®ããããšãã§ããŸãã
次ã«ã絶察å€ãæ±ããé¢æ°ãšã笊å·ãæ±ããé¢æ°ãèŠãŠã¿ãŸããããã¹ã¯ãªãããšãã£ã¿ã«ä»¥äžã®ããã«æžããŠå®è¡ããŠã¿ãŠãã ããã
abs(5 - 2)
abs(2 - 5)
sign(5 - 2)
sign(2 - 5)
![](https://assets.st-note.com/img/1706253797724-7xjoTAK2mh.png)
絶察å€ã¯ããã®åã®éãåŒæ°ã®å€ã®çµ¶å¯Ÿå€ãåºåãããŸãã笊å·é¢æ°ã¯ãåŒæ°ã®å€ãæ£ãªã1ãè² ãªã-1ã®å€ãåºåããŠãããŸãã
次ã«ãäžè§é¢æ°ãšåæ²ç·é¢æ°ãããããã®éé¢æ°ãæ±ããé¢æ°ã§ããããã§ã¯äžè§é¢æ°ãšéäžè§é¢æ°ã ããèŠãŠã¿ãŸããããã¹ã¯ãªãããšãã£ã¿ã«ä»¥äžã®ããã«æžããŠå®è¡ããŠã¿ãŠãã ããã
sin(pi/6)
cos(pi/6)
tan(pi/6)
asin(1/2)
acos(1/2)
atan(1/2)
![](https://assets.st-note.com/img/1706253870508-G4DbxKSxEO.png)
Rèšèªã§ãpiãã¯ååšçã®å€ãšããŠå®çŸ©ãããŠããŠããã®ãŸãŸå€æ°ãšããŠäœ¿çšã§ããŸãã
æåŸã«ãå®æ°ã®ç«¯æ°åŠçã«é¢ããé¢æ°ãèŠãŠã¿ãŸããããã¹ã¯ãªãããšãã£ã¿ã«ä»¥äžã®ããã«æžããŠå®è¡ããŠã¿ãŠãã ããã
value <- 10 / 3
value #3.333333
round(value)
floor(value)
ceiling(value)
trunc(value)
![](https://assets.st-note.com/img/1706254219905-QYjMSxsWvL.png)
ãããããåŒæ°ã«äžããå®æ°å€ã«å¯ŸããŠãå°æ°ç¹ä»¥äžããåæšäºå ¥ããåãäžãããåãæšãŠããæŽæ°éšåã ãåãåºãããšããæäœãããŠãããŸãã
æ°å€ããŸãšããŠãã¯ãã«ã§èšç®ããæ¹æ³
ã§ã¯æåŸã«ããã¯ãã«ã䜿ã£ãæ°å€èšç®ãè©ŠããŠã¿ãŸãããããããä»ã®ããã°ã©ãã³ã°èšèªãšæ¯ã¹ããšãã«ãçµ±èšåŠçã«åŒ·ãRèšèªã®ç¹åŸŽãšãããã®ããªãšæããŸãã
ç®è¡æŒç®ãããŠã¿ã
ãŸãããã¯ãã«ã䜿ã£ãŠè€æ°ã®æ°å€ã«ãŸãšããŠç®è¡æŒç®ãé©çšããŠã¿ãŸããã¹ã¯ãªãããšãã£ã¿ã«ä»¥äžã®ããã«æžããŠå®è¡ããŠã¿ãŠãã ããã
vec <- c(1, 2, 3 ,4, 5)
vec + 2
vec - 2
vec * 2
vec / 2
![](https://assets.st-note.com/img/1706254553402-aWjM85GlUQ.png)
cé¢æ°ã䜿ã£ãŠããã¯ãã«ãšããè€æ°ã®å€ãäžçµã«ããããŒã¿ãçæããŠããã®ã§ãããçæãããã¯ãã«ã¯å€æ°vecã«æ ŒçŽããŠããŸãããããŠããã®ãã¯ãã«ã®ããããã®å€ã«2ãå æžä¹é€ããçµæãäžèšã®ãã®ã«ãªããŸãã
次ã«ã2ã€ã®ãã¯ãã«ã«ç®è¡æŒç®ãé©çšãããšã©ããªãã§ããããïŒã¹ã¯ãªãããšãã£ã¿ã«ä»¥äžã®ããã«æžããŠå®è¡ããŠã¿ãŠãã ããã
vec1 <- c(1, 2, 3 ,4, 5)
vec2 <- c(5, 4, 3, 2, 1)
vec1 + vec2
vec1 - vec2
vec1 * vec2
vec1 / vec2
![](https://assets.st-note.com/img/1706254624523-D0THE8BjtZ.png)
ä»åã¯ãcé¢æ°ã䜿ã£ãŠå€æ°vec1ãvec2ã®äºã€ã®ãã¯ãã«ãçæããŠããŸããäºã€ã®ãã¯ãã«ãæŒç®ããçµæã¯ããã¯ãã«ã®ããããã®å€ã®åãé çªã«ãããã®ãå æžä¹é€ããŠãã圢ã«ãªããŸãã
æ°åŠé¢æ°ã䜿ã£ãŠã¿ã
ã§ã¯æåŸã«ããã¯ãã«ã«æ°åŠé¢æ°ãé©çšããŠã¿ããšã©ããªãã®ã§ããããïŒã¹ã¯ãªãããšãã£ã¿ã«ä»¥äžã®ããã«æžããŠå®è¡ããŠã¿ãŠãã ããã
vec <- c(1, 2, 3 ,4, 5)
exp(vec)
log(vec)
sin(vec)
cos(vec)
round(vec / 4)
trunc(vec / 4)
![](https://assets.st-note.com/img/1706254817885-Kq3FavBome.png)
é¢æ°ã®åŒæ°ã«ãã¯ãã«ãæå®ããŠããããšããã¯ãããããã®å€ã«å¯ŸããŠé¢æ°ãé©çšããçµæãåŸãããŸãã
ãã®ããã«ããã¯ãã«ã䜿ããšè€æ°ã®æ°å€ãæ±ãèšç®ãç°¡åãªããã°ã©ã ã§æžããã®ããRèšèªã®äŸ¿å©ã§é¢çœããšããã§ããããŒã¿æ°ãèšå€§ãªçµ±èšåŠçãªããã§è©ŠããŠã¿ããšããã®äŸ¿å©ããå®æã§ãããã§ããã
ãããã«
ä»åã¯ãRèšèªã®é¢æ°ãçšããŠæ°å€èšç®ãè¡ãããæ¹ã«ã€ããŠäž»ã«èŠãŠããŸããããã¯ãã«ã䜿ã£ãŠæ°å€ããŸãšããŠèšç®ããããæ¹ã«ã€ããŠãç°¡åã«è§ŠããŸããã
ãã ãããŸã§ã ãšãé¢æ°é»åã§è¡ãããããªç°¡åãªããšãã觊ããŠããªãã®ã§ããŸã ãŸã ããã°ã©ãã³ã°ã®å¿ èŠæ§ã¯æããããŸãããã
次ããã¯ãããäžæ©ããã°ã©ãã³ã°ã®åºç€çãªãšããã«èžã¿èŸŒãã§ãç解ãæ·±ããŠãããããšæããŸãã次åã¯ã¡ãã£ãšæŽæ°ããããŠããŸããšæãã®ã§ãããèå³æã£ãŠãã ããæ¹ã¯æ°é·ã«åŸ ã£ãŠããã ãããšå¬ããã§ãã
ãã®èšäºãæ°ã«å ¥ã£ãããµããŒããããŠã¿ãŸãããïŒ