ãä»ã®ã¹ã¯ãªããè¨èªãªããããªãã«è§¦ãããã©ãRã£ã¦å¤ãªæ©è½ãå¤ãããé£ãããã¨æãã¦ãã人ã®ããã«ãRã®ç¬ç¹ãªç¹ããããã©ãããããã¦ç¹ã«ãã¼ã¿ã®éè¨ã§ã®å¼·åãªæ©è½ãããã±ã¼ã¸ãã¾ã¨ãã¦ã¿ã¾ããã
社å ã®ãã¼ã¿ãªã½ã¼ã¹ç§»è¡ã®ä½æ¥ã®ä¸ã§ãåä»»è ãï¼è¯ãæå³ã§ãæªãæå³ã§ãï¼ãããRããã°ã©ãã¼ã ã£ãã®ã¨ãDBããã®ãã¼ã¿ã®åå¾ãããã¼ã¿ã®æ´å½¢ã»ç¢ºèªãä¸æ°ã«è¡ããã¨ããçç±ã§ä¹ ãã¶ãã«Rã触ãã¾ããã
ï¼å
輩ï¼åãã%>%
ã£ã¦ãªãã§ããï¼ãã¿ãããªè³ªåããã¨ããã£ããã§githubã®ãªã³ã¯ãè²¼ãããããAdvancedRãè¦ãããããããã®ããã£ã¡ãå°ãã¾ããï¼ï¼
æ°å¹´åRã使ã£ã¦ããã¨ãã¯ã
- Rã®ããã°ã©ã ã£ã¦ããããããªããªãâ¦ãå¾ããèªåã®ã³ã¼ãè¦ã¦ãæå³ãèªã¿åããªããâ¦
- Pythonã§pandasã¨ãmatplotlibã£ã¦ããã±ã¼ã¸ã使ãã°Rã£ã½ããã¨ãã§ããã®ï¼ããããã£ã¡ä½¿ãã£ã¨ï¼
ã¨æã£ã¦ããã®ã§ãããããã¤ãã®ããã±ã¼ã¸ãå©ç¨ãããã¨ã§ãï¼PythonãRubyã®ãããªï¼ã¹ã¯ãªããè¨èªã¦ã¼ã¶ã¼ã«ã¨ã£ã¦ããããããããRã®ã³ã¼ããæ¸ãããã¨ã«æ°ã¥ãã¾ãããããã ãã§ãªããRã®å¼·åãªéè¨æ©è½ããã¤ãæ¼ç®å%>%
ã¨çµã¿åããã楽ããããã°ã©ãã³ã°ãã§ããããã«ãªãã¾ããã
Rã®æ©è½ã¯å¼·åã§ããåãæ £ããªãã¨æ¸ãæ¹ãåãããªããä¸å¿æ¸ãããã©éå¹çãªæ¸ãæ¹ããã¦ãã¾ãã¨ãããã¨ãããã¨æãã¾ãã Rã£ã¦ããåãããªããªãã£ã¦æã£ã¦ããæ¹ã«ãå°ãã§ããå½¹ã«ç«ã¦ãã°å¹¸ãã ã¨æãã ç§ããã®ä¸ã¶æåãããã®éã«å¦ãã ãã¨ãå ±æãããã¨æãã¾ãã
Rè¨èªã®ç¹å¾´
Excelããä»ã®ããã°ã©ãã³ã°ã«ããããã¼ã¿ã®éè¨ãåæã¯ãã ããã以ä¸ã®æ§ãªæ®µéã«åããããã¨æãã¾ãã
- ãã¼ã¿ã®èªã¿åã
- ãã¼ã¿ã®å å·¥ã»éè¨
- ï¼ã¢ãã«åï¼
- ãã¼ã¿ã®æ¸ãåºãã»å¯è¦å
ã¾ãExcelã«ã¤ãã¦èããã¨ãã¾ããããã¼ã¿ã®èªã¿åããã®æ®µéã§ã¯.xlsxã.csvãªã©ã®å½¢å¼ãªãèªã¿è¾¼ãã¾ãããjsonãèªã¿è¾¼ãã®ã¯é£ãããã§ãã ããã¼ã¿ã®å å·¥ãã¯åé¡ãªãã§ãã¾ãããä¸åº¦è¡ã£ãä½æ¥ãä½åº¦ãç¹°ãè¿ãããã«ã¯ãã¯ããVBAã使ãå¿ è¦ãããã ããã°ã©ãã³ã°è¨èªã¨ãã¦ã¯å°ãé¢åã§ãã
Pythonãªã©ã®æ±ç¨ããã°ã©ãã³ã°è¨èªãèããã¨ããã¼ã¿ã®èªã¿åãã¯ç°¡åã«ã§ãã¾ããã ãã¼ã¿ã®å å·¥ãéè¨ã¯å°ã é¢åã§ãããã°ã©ãã使ã£ã¦å¯è¦åããã«ã¯ç¹å¥ãªã©ã¤ãã©ãªï¼Pythonãªãmatplotlibãªã©ï¼ãå¿ è¦ã ã£ãããã¾ãã
ãããè¸ã¾ããã¨ãRã§ã¯ãã¼ã¿è§£æã®ããã»ã¹ãå ¨é¨ä¸ã¤ã®è¨èªã§ããªããããã«ãªã£ã¦ããã ç¹ã«å¼·ã¿ã¯æ¬¡ã®3ã¤ã«ã¾ã¨ããããã¨æãã¾ãã
- ãã¼ã¿ã®å å·¥ãå§åçã«æ¥½(
dplyr
,tidyr
) - ãã¼ã¿ã®å¯è¦åãé«æ©è½
- æ¨æºã®ploté¢æ°ã§å¤§ä½ã®ãã¼ã¿ã¯å¯è¦åã§ãã
ggplot2
çã§ãããããªãããããä½ãã
- æ¨æºã®ploté¢æ°ã§å¤§ä½ã®ãã¼ã¿ã¯å¯è¦åã§ãã
- ææ°ã®çµ±è¨ã¢ãã«ãã©ã¤ãã©ãªã®å½¢ã§ããã«å ¬éããã
ä»ã®è¨èªããæ¥ã人ã¯ãæ¨æºã§ã°ã©ããããããããä¸è½ãªé¢æ°(plot()
ããã¹ãã°ã©ã ãhist()
ã ãã§æ¸ãã¾ã)ããã£ãããRèªä½ã«ããã±ã¼ã¸ãã¤ã³ã¹ãã¼ã«ããé¢æ°(install.package()
)ããããã¨ã«é©ãããããã¾ããã
ãã®ããããRã¯ããã°ã©ãã³ã°è¨èªã§ã¯ãªãçµ±è¨ã®ããã®éçºï¼å®è¡ç°å¢ã§ãããã®ããã«è¨ããããã¨ãããã¾ãã
Rã®æ å ±æºã»å ¥éæ¸
ã¾ãã¯ãRã®åºæ¬çãªä½¿ãæ¹ã説æãã¾ãã ã¨ã¯ãã£ã¦ããç´é¢ã®é½åä¸ãä»è¨èªã®ããã°ã©ãã¼ã«ã¨ã£ã¦éåæã®ããã¨ãããã¤ã¾ã¥ããããã¨ããããç´¹ä»ãã¾ããã ä¸åº¦Rã®æ¬ãèªãã§ã¿ã¾ãããã
ååã®Rããã°ã©ãã¼ã«ãä½ãããå
¥éæ¸ã¯ãªãããã¨è³ªåããã¨ããã
ãæ°ç±³æ¢åµããã¼ã¿åæã«æããã¨ããæ¬ãæãã¦ãããã¾ããã
dplyr
ãªã©ã®ã©ã¤ãã©ãªã使ã£ããä»ã¾ã§ã®Rã®æ¬ããããããããï¼æè¿ã®ä¸»æµã®ï¼æ¸ãæ¹ãç´¹ä»ããã¦ãã¾ãã
æ°ç±³æ¢åµããã¼ã¿åæã«æã
- ä½è : ç³ç°åºåº,shimano
- åºç社/ã¡ã¼ã«ã¼: SBã¯ãªã¨ã¤ãã£ã
- çºå£²æ¥: 2015/10/28
- ã¡ãã£ã¢: åè¡æ¬
- ãã®ååãå«ãããã° (4件) ãè¦ã
Rã®éçºç°å¢ã¯RStudioãããããã§ãã ã¤ã³ã¹ãã¼ã«æ¹æ³ãªã©ã¯ãã®è¨äºã§ã¯ç´¹ä»ããªãã®ã§ãä¸ã®ãµã¤ããåèã«ãã¦ãã ããã
ã¾ããï¼ä»ã®ä¸æåã®ååã®è¨èªã§ãåãã ã¨æãã¾ããï¼Rã¯Googleã§æ¤ç´¢ãã¥ããã§ãã Rã®ããã®æ¤ç´¢ã¨ã³ã¸ã³ã®seekRã¨ãããã®ãããã®ã§ãããããªãç¹ãããã°ããã§èª¿ã¹ã¾ãããã
ã¾ããé¢æ°ã®ä»æ§ãããããããªãã¨ãã¯ãhelp
é¢æ°ã便å©ã§ãã
ä¾ãã°print
é¢æ°ã«å¯¾ãã¦å®è¡ããã¨ã
help(print)
ãã®ããã«ããã¥ã¡ã³ãã表示ããã¾ãã
使ã£ã¦ã¿ããï¼
ã¾ããRStudioãéãã¦ã³ã³ã½ã¼ã«ã«æ¬¡ã®ããã«æã¡è¾¼ãã§ã¿ã¾ãããã
1 + 1 # [1] 2
2ã¨ããå¤ãè¿ã£ã¦ãã¾ãããRã®REPLã¯é»åã¨ãã¦ä¾¿å©ã§ãã飲ã¿ä¼ã®ã¯ãªã«ã³ã®è¨ç®ã«ã使ãã¾ãããã
次ã«ãiris
ã¨æã¡è¾¼ãã§ã¿ã¾ããããRã«ã¯ãããããªãã¹ããã¼ã¿ãããã¤ãæåã«å
¥ã£ã¦ãã¦ãããã®é¢æ°ã£ã¦ã©ãããåãããããã ã£ãï¼ãã£ã¦æ©ãã ã¨ãã«ãã³ã³ã½ã¼ã«ã使ã£ã¦ç¢ºããããã¨ãã§ãã¾ãã
iris # Sepal.Length Sepal.Width Petal.Length Petal.Width Species # 1 5.1 3.5 1.4 0.2 setosa # 2 4.9 3.0 1.4 0.2 setosa # 3 4.7 3.2 1.3 0.2 setosa # 4 4.6 3.1 1.5 0.2 setosa # 以ä¸ç¥
150è¡ãããã¼ã¿ãåºã¦ãã¾ããããã¯Rã®ãã¼ã¿ãã¬ã¼ã ã¨å¼ã°ããåã§ããã¼ã¿ãã¼ã¹ã®ãã¼ãã«ã®ãããªæä½ãç°¡åã«ã§ãã¾ãããã®ãã¼ã¿ãã¬ã¼ã ã®æä½ã«ãã£ã¦ãã¼ã¿ã®éè¨ãç°¡åã«ã§ããã®ãRã®æ大ã®å¼·ã¿ã§ããç¹ã«å¾ã§ç´¹ä»ããdplyr
ããã±ã¼ã¸ã使ãã¨æå¼·ã§ãã
ã¡ãªã¿ã«ãã®iris
ãã¼ã¿ã¯ãFisherã¨ããåãå¦è
ããã®ç 究ã§ä½¿ããããã¼ã¿ã»ãããããã§ãã
ååï¼ã«ã©ã ï¼ã®ãã¼ã¿ã ããè¦ãã«ã¯ã$
ã使ã£ã¦
iris$Sepal.Length # [1] 5.1 4.9 4.7 4.6 5.0 5.4 4.6 5.0 4.4 4.9 5.4 4.8 4.8 4.3 5.8 5.7 5.4 5.1 5.7 5.1 5.4 5.1 4.6 5.1 4.8 # [26] 5.0 5.0 5.2 5.2 4.7 4.8 5.4 5.2 5.5 4.9 5.0 5.5 4.9 4.4 5.1 5.0 4.5 4.4 5.0 5.1 4.8 5.1 4.6 5.3 5.0 # [51] 7.0 6.4 6.9 5.5 6.5 5.7 6.3 4.9 6.6 5.2 5.0 5.9 6.0 6.1 5.6 6.7 5.6 5.8 6.2 5.6 5.9 6.1 6.3 6.1 6.4 # [76] 6.6 6.8 6.7 6.0 5.7 5.5 5.5 5.8 6.0 5.4 6.0 6.7 6.3 5.6 5.5 5.5 6.1 5.8 5.0 5.6 5.7 5.7 6.2 5.1 5.7 # [101] 6.3 5.8 7.1 6.3 6.5 7.6 4.9 7.3 6.7 7.2 6.5 6.4 6.8 5.7 5.8 6.4 6.5 7.7 7.7 6.0 6.9 5.6 7.7 6.3 6.7 # [126] 7.2 6.2 6.1 6.4 7.2 7.4 7.9 6.4 6.3 6.1 7.7 6.3 6.4 6.0 6.9 6.7 6.9 5.8 6.8 6.7 6.7 6.3 6.5 6.2 5.9
å¤ãåããã¨ãã§ãã¾ãããã¡ãªã¿ã«Sepal.Length
ã¨ããå¤æ°åã«é©ããæ¹ãããã¨æãã¾ãããRã§ã¯å¤æ°åã«ããã"."ã使ãã¾ããä¸æè°ã¡ããã§ããã
ã¾ãã[]
ã使ã£ã¦æ¬¡ã®ããã«ã«ã©ã ãåãåºããã¨ãã§ãã¾ãã
iris['Sepal.Length'] # Sepal.Length # 1 5.1 # 2 4.9 # 3 4.7 # ï¼ä»¥ä¸ç¥ï¼
$
ã®å ´åã¨åºåå½¢å¼ãå°ãéãã¾ãããããã¯$
ã®ã¨ãã¯ã«ã©ã ã®ä¸ã«ãããã¯ãã«ï¼ãã¯ãã«ã«ã¤ãã¦ã¯å¾ã§èª¬æãã¾ãï¼ã§åãåºãããã®ã«å¯¾ãã[]
ã使ã£ãã¨ãã¯Sepal.Length
ã®ã«ã©ã ã ãã®ãã¼ã¿ãã¬ã¼ã ã§åãåºããã¾ãã
Rã®ããã°ã©ã ã¯ãã»ã¨ãã©ã®å ´åãcsvããã¼ã¿ãã¼ã¹ã®å¤ããã¼ã¿ãã¬ã¼ã ã®å¤æ°ã«æ ¼ç´ãã¦å©ç¨ãã¾ãã
ä¾ãã°ãcsvã®ãã¼ã¿ãèªã¿è¾¼ãã«ã¯read.csv
ã¨ããé¢æ°ã使ãã¾ãã
example_df <- read.csv('./example.csv')
http://cse.naro.affrc.go.jp/takezawa/r-tips/r/40.html
éåæ1ï¼å¤æ°ã®ä»£å ¥ã"<-"
foræãifæãªã©ãåºæ¬çãªæ§æãç´¹ä»ãã¦ãã£ã¦ãããã®ã§ãããä»ã®è¨èªããã£ã人ãªãã»ã¨ãã©è©°ã¾ããã¨ã¯ãªãã¨æãã¾ãã ãã ããRã«ã¯å¤ãªæ§æãå¤ãã®ã§ãéåæããã¨æãã¨ãããããã¤ãããã¯ã¢ãããã¦ã¿ã¾ãã
ã¾ããå¤æ°ã®ä»£å
¥ãã=
ã§ãªã<-
ã¨ãããã¨ãã¾ãã->
ã§å·¦ããå³ã«ä»£å
¥ã§ãã¾ãããã§ããå®ã¯=
ã§ã代å
¥ã§ãã¦ãã¾ãã¾ããä¸æè°ã¡ããã§ããã
x <- 'hello R!' x # [1] "hello R!" # å®ã¯=ã§ãä»£å ¥ã§ãããéæ¨å¥¨ x = 'hello R!' x # [1] "hello R!" # å®ã¯ããã§ãå·¦ããå³ã«ä»£å ¥ã§ããï¼ 1 + 1 -> y y # [1] 2
ã¾ããé¢æ°ã¯é¢æ°ãä»£å ¥ããå½¢ã§å®£è¨ãã¾ããJavaScriptã®é¢æ°å¼ã¿ãããªæãã§ãã
add <- function(x, y) { return (x + y) } add(1, 2) # 3 # æ示çã«returnãæ¸ããªãã¦ã大ä¸å¤«ã§ã add <- function(x, y) {x + y}
éåæ2ï¼ãã¯ãã«ã¨ãªã¹ã
ã¾ããé åã£ã½ããã¼ã¿æ§é ã«ãã¯ãã«ã¨ãªã¹ãã¨ããäºç¨®é¡ãããã¾ãã
c(1, 2, 3) list(1, 2, 'a')
ãã®2ã¤ã¯ã©ãéãã®ã§ããããï¼å®ã¯ããã¯ãã«ã¯ãåãåã®å¤ãå
¥ããã¨ããå¶éããããåãæä½ããã¯ãã«å
¨ä½ã«ãããæä½ãã§ãã¾ããPythonãRubyã®map
ã¿ãããªã¤ã¡ã¼ã¸ã§ããããã«ãã£ã¦æ°å¤è¨ç®ããã¼ã¿è§£æããã¡ããã¡ã楽ã«ãªãã¾ãã
# ãã¯ãã«å ¨ä½ã«1ã足ã c(1, 2, 3) + 1 # [1] 2 3 4 # ãã¯ãã«ã®åè¦ç´ ã足ãä¸ãã c(1, 2, 3) + c(10, 20, 30) # [1] 11 22 33
ä»ã«ããæ°å¦ã®å ç©ã¨ããã®è¾ºã®æä½ãã§ãã¾ãã
ãã®ãããRã§ã¯ã»ã¨ãã©foræãæ¸ããã¨ã¯ãªãããã¯ãã«æ¼ç®ã§æä½ãããã¨ãæ¨å¥¨ããã¦ãã¾ãã Rã®foræã¯ä»è¨èªã«æ¯ã¹ã¦ãé ãããã§ããã
Rã®å¤ãã®é¢æ°ã¯ããã¯ãã«ã«å¯¾ãã¦ä¸æ°ã«æä½ã§ãã¾ãã
# èåãåãæä½ãé¢æ°ãã¾ã¨ãã get_family_name <- function(name) { # ã¹ãã¼ã¹ã®ä½ç½®ãåãåºã' ' name_seq <- regexpr(" ", name) # èåãåãåºã family_name <- substr(name, 1, name_seq - 1) return (family_name) } # èåãåå¾ãã get_family_name('hoshimiya ichigo') # [1] "hoshimiya" # ãã¯ãã«ã«å¯¾ãã¦ä¸æ°ã«æä½ã§ãã get_family_name(c('hoshimiya ichigo', 'kiriya aoi', 'shibuki ran')) # [1] "hoshimiya" "kiriya" "shibuki"
å®ã¯ãæ°å¤è¨ç®ã©ã¤ãã©ãªã§ããnumpyã使ãã°ãPythonã§ãåããããªãã¯ãã«åãå©ç¨ãããã¨ãã§ãã¾ãã Rubyã«ããããããªæ°ããã¾ããã詳ããã¯ç¥ãã¾ããâ¦ã
ã¾ããæååã¨æ°åãæ··ãã¦ãã¯ãã«ã宣è¨ããã¨ã©ããªãã®ã§ããããï¼
c(1, 2, 'a') # [1] "1" "2" "a"
ã¨ã©ã¼ãçºãããå
¨é¨æåååã«å¤æããã¦ãã¾ãã¾ããã
Rã§ã¯ã表ç¾åã®å¼·ãåã«å¤æããããã¨ããã«ã¼ã«ã§æé»ã®åå¤æãè¡ããã
ä¾ãã°æååã¨æ´æ°ã§ã¯æååã«ãæ´æ°ã¨å®æ°ã§ã¯å®æ°ã«å¤æããã¾ãã
ï¼æ¯è¼æ¼ç®==
ã§ãåæ§ãªã®ã§è¦æ³¨æã§ãï¼ã
人ã«ãã£ã¦ã¯PHPã®ãã©ã¦ããå¼ã³èµ·ããããã¨æãã¾ããã åã«ã¤ãã¦ã»ã¨ãã©èããªãã¦è¯ãã®ã§ãä¾ãã°csvããèªã¿è¾¼ãã ãã¼ã¿ãã¢ãããã¯ãªåæãããéã«ã¯æ¥½ã§ãã ï¼éã«ãå³å¯ã«ä¾å¤å¦çããã·ã¹ãã ãä½ããã¨ã«ã¯åãã¦ãªãããããã¾ããï¼ã
ä¸æ¹ããªã¹ãã¯åãæ··ãã¦è¦ç´ ãå ¥ãããã¨ãã§ãã¾ãã
list(1, 2, 'a') # [[1]] # [1] 1 # # [[2]] # [1] 2 # # [[3]] # [1] "a"
ã¾ãããªã¹ãã®åè¦ç´ ã«ååãã¤ãããã¨ãã§ããé£æ³é åã®ããã«ä½¿ããã¨ãã§ãã¾ãã
aikatsu <- list(soleil = c('ichigo', 'aoi', 'ran'), powa2puririn = c('otome', 'sakura', 'shion')) aikatsu$soleil # [1] "ichigo" "aoi" "ran"
ãã ãããã¼ãã¦ãã¼ã¯ã§ããã¨ããä¿è¨¼ã¯ããã¾ããã
aikatsu <- list(soleil = c('ichigo', 'aoi', 'ran'), soleil = c('hoshimiya', 'kiriya', 'shibuki')) aikatsu # $soleil # [1] "ichigo" "aoi" "ran" # # $soleil # [1] "hoshimiya" "kiriya" "shibuki" # `$`ã§ã¯æåã®è¦ç´ ã«ããã¢ã¯ã»ã¹ã§ããªã aikatsu$soleil # [1] "ichigo" "aoi" "ran"
å®ã¯ãããã¯Rubyã®ããã·ã¥ãPythonã®è¾æ¸ã¨ã¯ç°ãªããlistã®ã«ãã¯ã¢ããã¨ããæ©è½ã§è¨ç®ãé
ãããã§ãã
ä¸å¿ãhash
ã¨ããã©ã¤ãã©ãªã§Rubyã®ããã·ã¥ã¿ãããªãã®ã使ããããã«ãªãããã§ããã
ãããããä¸ã¤å¤§äºãªãã¨ãããå¿ãã¦ãã¾ããã Rã®ãªã¹ãããã¯ãã«ã®ã·ã¼ã±ã³ã¹ã¯ãã»ã¨ãã©ã®è¨èªã¨ç°ãªãã0ãã§ã¯ãªãã1ãããå§ã¾ãã¾ãã
a <- c(1, 2, 3) a[1] # [1] 1 a[2] #[1] 2 a[0] # numeric(0)
ããã¯ãRããCè¨èªã§ã¯ãªãæ°å¤è¨ç®ç¨ã®Fortranããå¼·ãå½±é¿ãåããããã ããã§ãã ä»ã«ããæ°å¤è¨ç®è¨èªã®MATLABãJuliaã§ãé åã¯1ããå§ã¾ãã¾ãã ãããã«ãæ°å¦ã§ã第ä¸æåãã第äºæåãã¨æ°ãã¦ãããã®ãç´ ç´ã«ã³ã¼ãã«è½ã¨ãè¾¼ããã®ã§ããã ä»ã®è¨èªã¨ã®å¯¾å¿ãèããã¨ãï¼ä¾ãã°Rã®ã³ã¼ããPythonã®numpyã«ç§»æ¤ãããå ´åãªã©ï¼ã«ã¯é¢åã§ãã
ããã«ãã¯ãã«ããªã¹ãã«ã¤ãã¦è©³ãã話ã¯ããã¡ãã®ãµã¤ããåç §ãã¦ãã ããã http://cse.naro.affrc.go.jp/takezawa/r-tips/r/23.html
å®ã¯ãå ã»ã©è©±ã«åºããã¼ã¿ãã¬ã¼ã ã¯ãããã¯ãã«ã«å ¥ã£ããªã¹ããã ããã§ãï¼Rè¨èªã®åèãAdvanced Rãã«æ¸ãã¦ã¾ããï¼ã ãã®ãããå ã»ã©ãã¯ãã«ã®è©±ã§åºãããã«ãããã¼ã¿ãã¬ã¼ã ã®åã«ã©ã ï¼ãã¯ãã«å ¨ä½ï¼ã«å¯¾ãã¦åãæä½ãè¡ããã¨ããæä½ãç°¡åã«è¡ããã¨ãã§ãã¾ãã
Advanced Rã¯ä»åº¦æ¥æ¬èªè¨³ãåºããããªã®ã§æ¥½ãã¿ã§ããã
- ä½è : Hadley Wickham,ç³ç°åºåº,å¸å·å¤ªç¥,é«æ³æ ä¸,ç¦å³¶ç太æ
- åºç社/ã¡ã¼ã«ã¼: å ±ç«åºç
- çºå£²æ¥: 2016/02/10
- ã¡ãã£ã¢: åè¡æ¬
- ãã®ååãå«ãããã° (26件) ãè¦ã
éåæ3ï¼ å ¨ã¦ãããã¼ã¿ãã¨ãé¢æ°ã
Rã®ä¸çã¯ããã¼ã¿ã¨é¢æ°ã§æ§æããã¦ãã¾ãã
ä»ã®è¨èªã§ãæãã®æ©è½ã®ã»ã¨ãã©ï¼ããããå
¨ã¦ï¼ããé¢æ°ã¨ãã¦å®è£
ããã¦ãã¾ãã
ä¾å¤å¦çãtry
é¢æ°ãããã¯tryCatch
é¢æ°ã使ãã¾ãã
例外処理 | Rのtry関数とtryCatch関数による例外処理
ä¸é
æ¼ç®åã®ãããªãã®ããifelse
é¢æ°ã使ãã¾ãã
x <- NA # NAã¯ãæªå®ç¾©ãã¨ããæå³ ifelse(is.na(x), 0, x) # [1] 0 x <- 1 ifelse(is.na(x), 0, x) # [1] 1
å®ã¯ã+
æ¼ç®åãªã©ãé¢æ°ã§ããã`ãã§å²ããã¨ã«ãã£ã¦é常ã®é¢æ°ã®ããã«ã使ãã¾ãã
ä¸ç½®ã®æ¼ç®åãé¢æ°ã¨ãã¦ã使ããã£ã¦ããã®ã¯Haskellã£ã½ãã§ããã
`+`(1, 2) # [1] 3 # é«éé¢æ°ã§ä½¿ããã¨ãã§ããã Reduce(`+`, c(1, 2, 3)) # [1] 6 # 2å¼æ°ã®é¢æ°ã¨ãã¦æ¼ç®åãå®ç¾©ãããã¨ãã§ãã "&" <- function(chr1, chr2) { paste(chr1, chr2, sep = '') } 'tom' & 'jerry' # [1] "tomjerry"
ã¾ããå¯å¤é·å¼æ°ã®é¢æ°ããã¡ããã¡ãå¤ããã¨ãç¹å¾´ã§ãã
代表çãªãã®ããæååçµåã®é¢æ°ã®paste
ã§ãã
# ãªããããã©ã«ãã§ã¯ã¹ãã¼ã¹åºåãã§åºåããã paste('SELECT', '*', 'FROM', 'example_table') # [1] "SELECT * FROM example_table" paste('SELECT', '*', 'FROM', 'example_table', sep = '') # [1] "SELECT*FROMexample_table" # ããã©ã«ãã®åºåãæåã空æåã®ãpaste0é¢æ°ããã [1] "SELECT*FROMexample_table" # [1] "SELECT*FROMexample_table"
ãã®ããã«ãRã¯ä¼¼ãæ©è½ã®é¢æ°ãå¤æ°ãã£ãããå身ã«ãªã£ãSè¨èªã®é¢æ°åãæ®ã£ã¦ãããããã®ã§ã å°ã ãããã¥ããã¨ãããããã¾ãã ç¹ã«ã»ãã®äººã«ããã°ã©ã ã渡ãã¨ãã¯æ³¨æãã¹ãã§ãããã
dplyr, tidyrã«ãããã¼ã¿ã®æ´å½¢
ãã¼ã¿ã®æ´å½¢ã«ã¯dplyr
ã¨ããããã±ã¼ã¸ã使ãã¾ãããã
ç§ã¯ä»¥åãRã§ã®ãã¼ã¿ãã¬ã¼ã ã®æä½ã£ã¦é£ãããªãâ¦SQLãªãã©ãéè¨ããã°ããã®ããããã®ã«ãã¨æã£ã¦ããã®ã§ããã
dplyr
ã使ããã¨ã§æãéãã®éè¨ãè¡ããããã«ãªãã¾ããã
å®ã¯ãdplyr
ã¯ããæ°å¹´ã®éã«ç¾ããããã±ã¼ã¸ã§ãRã®ããã°ã©ãã¼ã®éã§åºã使ããã¦ããããã±ã¼ã¸ã§ãã
ããæï¼2ã3å¹´åï¼Rã«è§¦ãã¦ãæè¿Japan.Rãªã©ã®åå¼·ä¼ã«åå ããããã«ãªã£ã人ãããã°ã
ã¾ãçºè¡¨ã®ä¸ã§(dplyr
ã§ä½¿ããã)ãã¤ãæ¼ç®å%>%
ãä¹±èãã¦ãããã¨ã«é©ãã¨æãã¾ãã
dplyr
ã¨ããããã±ã¼ã¸ã¯ã次ã®ãããªãã®ã§ãã
- Rã®ãã¼ã¿ãã¬ã¼ã ã«å¯¾ãã¦SQLã®ãããªæä½ãç°¡åã«ã§ããã
- ãã¤ãæ¼ç®å
%>%
ã«ãã£ã¦ãã·ã§ã«ã¹ã¯ãªããã®ãã¤ããRubyã®ã¡ã½ãããã§ã¼ã³ã®ãããªä½¿ç¨æã
ããã¯ã@matsuou1ããã®ç´ æ´ãããè¨äºãããã®ã§ãã¾ãã¯ãã¡ããèªãã§ãã ããã
ã¾ãã¯ããã¤ãæ¼ç®å%>%
ã®èª¬æãããå¾ãdplyr
ã®æä½ã説æãã¾ãã
dplyrã§ä½¿ããããã¤ãæ¼ç®åã®èª¬æ
é¢æ°ã®èª¬æã®ã¨ããã§ããå®ã¯Rã§ã¯æ¼ç®åãï¼é¢æ°ã¨ãã¦ï¼ç°¡åã«å®ç¾©ãããã¨ãã§ããããã
ã©ã¤ãã©ãªã«ãã£ã¦ã¯ä¾¿å©ãªæ¼ç®åãèªã¿è¾¼ã¾ãããã¨ãããã¾ãã
ï¼ãã ããå®ã¯ãã¤ãæ¼ç®åã¯dplyr
ã®ãªãªã¸ãã«ã§ã¯ãªããå
é¨ã§magrittr
ã¨ããå¥ã®ããã±ã¼ã¸ãå¼ã³åºããã¦ããããã§ããï¼
å®éã«ããã¤ãæ¼ç®å%>%
ã使ã£ã¦ã¿ã¾ãããã
RStudioãªãããã¤ãæ¼ç®åã¯Ctrl + Shift + M
(MACãªãcommand + Shift + M
)ã®ã·ã§ã¼ãã«ããã§å
¥åã§ãã¾ãã
# dplyrããã±ã¼ã¸ãèªã¿è¾¼ãã§ãã library(dplyr) # headé¢æ°ã¯ãä¸çªç®ã®å¼æ°(iris)ã®ä¸ããäºçªç®ã®å¼æ°(2)è¡ãåã£ã¦ãããã¨ããæå³ head(iris, 2) # Sepal.Length Sepal.Width Petal.Length Petal.Width Species # 1 5.1 3.5 1.4 0.2 setosa # 2 4.9 3.0 1.4 0.2 setosa # ãã¤ã %>% ã使ããã¨ã§æ¬¡ã®ããã«æ¸ãã¾ã iris %>% head(2) # Sepal.Length Sepal.Width Petal.Length Petal.Width Species # 1 5.1 3.5 1.4 0.2 setosa # 2 4.9 3.0 1.4 0.2 setosa # gsubã¯æååç½®æã®é¢æ° gsub('::DATE::', '20160126', "SELECT * FROM example WHERE log_date = ::DATE::") # [1] "SELECT * FROM example WHERE log_date = 20160126" # ãã¤ãã§æ¸¡ãããå¤ã第ä¸å¼æ°ã§ãªãå ´åã¯ãä»ã®å¼æ°ã®ååãæ示ããã°OK "SELECT * FROM example WHERE log_date = ::DATE::" %>% gsub(pattern ='::DATE::', replacement = '20160126') # [1] "SELECT * FROM example WHERE log_date = 20160126" # ãã¤ãã§æ¸¡ã£ã¦ããå¤ããæ´ã«ãã¤ãã§æ¸¡ã "SELECT * FROM ::TABLE:: WHERE log_date = ::DATE::" %>% gsub(pattern ='::DATE::', replacement = '20160126') %>% gsub(pattern ='::TABLE::', replacement = 'example_log') # [1] "SELECT * FROM example_log WHERE log_date = 20160126"
%>%
ã¯ããæ¼ç®åã®å·¦ã®å¤(iris
)ããå·¦ã®é¢æ°ã®ç¬¬ä¸å¼æ°(head
)ã«ä»£å
¥ããå¤ãè¿ããã¨ãããã®ã§ãã
F#ã®æ¼ç®åããçºæ³ãå¾ããã®ã¨èãã¦ãã¾ãã
ããã ãã§ã¯ãç´ ç´ã«ãhead(iris, 2)
ã£ã¦æ¸ãã°ãããããï¼ä½ãå¬ããã®ï¼ãã¨ããæãã§ããã
ã·ã§ã«ã¹ã¯ãªãããRubyã®ã¡ã½ãããã§ã¼ã³ã®ããã«ã次ã®å¦çãå³ã«å³ã«ç¹ããã¨ãã§ãã¾ãã
ç¹ã«ãdplyr
ã®é¢æ°ã¨ä½µãã¦ä½¿ããã¨ã§ãé¢åãªéè¨ãç°¡åã«çµãããããã¾ãã
dplyrã«ãããã¼ã¿ãã¬ã¼ã ã®æä½
@matsuou1ããã®è¨äºã«ããSQLã®æä½ã¨ã®å¯¾å¿è¡¨ãããããããã§ãã
SQL | dplyr | R | 説æ |
---|---|---|---|
where | filter | subset | è¡ã®çµã込㿠|
count , max ,minç | summarise | aggregate | éè¨ãã |
group by | group_by | - | ã°ã«ã¼ãã³ã°ãã |
order by | arrange | sort , order | è¡ã並ã¹æ¿ãã |
select | select | data[,c("a","b")] | ãã¼ã¿ãã¬ã¼ã ããæå®ããåã®ã¿æ½åºãã |
(selectå¥) | mutate | transform | åã®è¿½å |
詳ãã使ãæ¹ã¯ãã¯ã@matsuou1ããã®è¨äºãèªãã§æ¬²ããã®ã§ããã ç°¡åã«ä½¿ç¨æã ã説æãã¾ãã
ä¾ã«ãã£ã¦ãiris
ãã¼ã¿ã使ã£ã¦èª¬æãã¾ãã
iris %>% head() # Sepal.Length Sepal.Width Petal.Length Petal.Width Species # 1 5.1 3.5 1.4 0.2 setosa # 2 4.9 3.0 1.4 0.2 setosa # 3 4.7 3.2 1.3 0.2 setosa # 4 4.6 3.1 1.5 0.2 setosa # 5 5.0 3.6 1.4 0.2 setosa # 6 5.4 3.9 1.7 0.4 setosa
ããã(iris)ã®å種(Species)ãã¨ã«ãè±ã³ãã®é·ã(Petal.Length)ã®å¹³åå¤ãåºãããã¨ãã SQLãªã次ã®ããã«ç°¡åã«éè¨ã§ããã¨æãã¾ãã
-- å¤æ°åã«ããããããã®ã¯éåæããã¨æãã®ã§ããã£ã¡ã«ã±ã¼ã¹ã«ãã¾ãã SELECT species, mean(patal_width) AS mean FROM iris GROUP BY species
ãããdplyr
ã§æ¸ãã¨æ¬¡ã®ããã«ãªãã¾ãã
library(dplyr) iris %>% dplyr::group_by(Species) %>% dplyr::summarise(mean = mean(Petal.Length)) # Source: local data frame [3 x 2] # # Species mean # (fctr) (dbl) # 1 setosa 1.462 # 2 versicolor 4.260 # 3 virginica 5.552 # Rã§ã¯ã©ã¤ãã©ãªåãçç¥ãããã¨ãã§ãã iris %>% group_by(Species) %>% summarise(mean = mean(Petal.Length))
ãªãã¨ãããã¨ã§ãããï¼ãã¤ãã使ã£ã¦2ã¤é¢æ°ã«æ¸¡ãã ãã§ãSQLã®ãããªæä½ãå®äºãã¦ãã¾ãã¾ããã
ä½è«ã§ãããdplyr
ã®é¢æ°ã¯ãã¤ãã使ããã«æ¸ããã¨ãã§ãã¾ãã
å
ã»ã©ã®å¦çã¯ãå®ã¯
summarise(group_by(iris, Species), mean = mean(Petal.Length))
ã¨åãæå³ãªã®ã§ãããç´ ç´ã«ãã¤ãã使ã£ãã»ããèªã¿ãããã¨æãã¾ãã
tidyrã«ãããã¼ã¿ã®å¤å½¢
dplyr
ã¯ããSQLã¨åãæä½ãããã°ã©ãã³ã°è¨èªã§ãå®ç¾ã§ãããã¨ããããã±ã¼ã¸ã§ããã
tidyr
ã¯ãSQLã§ã¯é£ããæä½ãç°¡åã«ã§ãããã¨ããããã±ã¼ã¸ã§ãã
å人çã«ã¯ãExcelã®ãããããã¼ãã«ã¨ä½¿ãã©ãããä¼¼ã¦ããã®ããªã¨æãã¾ãã
ã¾ãã次ã®ãããªPVæ°ã®ãã¼ã¿ãããã¨ãã¾ãã
ããããã®æ¥ã«ããµã¤ãã®PVæ°ãé½éåºçãã¨ã«ãããã ã£ãããã¨ãããã¨ã表ãã¦ããã¨æã£ã¦ãã ããã
ã¡ãªã¿ã«ãé½éåºçid(pref_id
)ã®13çªã¯æ±äº¬ã14çªã¯ç¥å¥å·ã§ãã
testdata <- data.frame( date = c('11/25', '11/25', '11/26', '11/26', '11/27'), pref_id = c(13, 14, 13, 14, 13), pv = c(1, 2, 3, 4, 5) ) testdata # date pref_id pv # 1 11/25 13 1 # 2 11/25 14 2 # 3 11/26 13 3 # 4 11/26 14 4 # 5 11/27 13 5
ã¢ã¯ã»ã¹è§£æãã¼ã«ã®ãã¼ã¿ãã¢ã¯ã»ã¹ãã°ãããããã¾ã§éè¨ããã®ã¯SQLã§ãdplyr
ã§ãç°¡åã ã¨æãã¾ãã
ãããã次ã«éè¨ãããå½¢ã¯ã次ã®ãããªãã®ã«ãªãã¨æãã¾ãã
date | æ±äº¬ã®PVæ° | ç¥å¥å·ã®PVæ° |
---|---|---|
11/25 | 1 | 2 |
11/26 | 3 | 4 |
11/26 | 5 | 0 |
SQLã§è¡ãå ´åã次ã®ããã«ãªãã¨æãã¾ãããçã®æ°ãå¢ããå ´åãä¼¼ããããªã«ã©ã ãä½åº¦ãæ¸ãå¿ è¦ããã£ã¦é¢åã§ãã DBã®å¤ãã¡ãã£ã¨ç¢ºèªããã®ã«ããDBã¯ã©ã¤ã¢ã³ãããcsvãã¨ã¯ã¹ãã¼ãããExcelã®ãããããã¼ãã«ã§éè¨ãããããªä½æ¥ãå ¥ã£ã¦ãã¾ãããããã¾ããã
SELECT date, SUM(CASE WHEN pref_id = 13 THEN pv ELSE 0 END)) AS Tokyo_pv, SUM(CASE WHEN pref_id = 14 THEN pv ELSE 0 END)) AS Kanagawa_pv FROM testdata GROUP BY date
ãã®ãããªéè¨ã¯ãtidyr::spread
ã¨ããé¢æ°ã§ç°¡åã«è¡ãã¾ãã
library(tidyr) testdata %>% tidyr::spread(key = pref_id, value = pv) # date 13 14 # 1 11/25 1 2 # 2 11/26 3 4 # 3 11/27 5 NA # ããã©ã«ãã§ã¯å¤ãå ¥ããªãã¨ããã«NA(æªå®ç¾©å¤)ãå ¥ãããã0ã§è£å®ããå ´åã¯æ¬¡ã®ããã«æ¸ã testdata %>% tidyr::spread(key = pref_id, value = pv, fill = 0) # date 13 14 # 1 11/25 1 2 # 2 11/26 3 4 # 3 11/27 5 0
tidyr
ã®è©³ãã使ãæ¹ãä»ã®é¢æ°ãç¥ãããæ¹ã¯ã次ã®è¨äºãããããã§ãã
purrrã«ããé¢æ°åããã°ã©ãã³ã°
ãã¤ãã«ããã¡ã½ãããã§ã¼ã³ãè¦ããã¨ããªã¹ãããã¯ãã«ã«å¯¾ãã¦Rubyã®mapã¡ã½ããã®ãããªãã¨ããããããªãã¨æãã¾ãã
ãã¡ãã®è¨äºã«ããã qiita.com
- [2,4,6,8,10]ã¨ããé åãç¨æãã¦
- âã®é åãã8以ä¸ã®æ°ã ããé¸æããé åãä½ã
- âã®é åããåè¦ç´ ãï¼ä¹ããé åãä½ã
- âã®é åãã20以ä¸ã®æ°ã ããé¸æããé åãä½ã
- âã®é åã®å¤ãæãåããã
ã¨ããæä½ãR + purrr
ã§è¡ã£ã¦ã¿ããã¨æãã¾ãã
library(purrr) c(2, 4, 6, 8, 10) %>% keep(~. <= 8) %>% map(~. ** 2) %>% keep(~. >= 20) %>% reduce(`*`)
Rubyã®ã³ã¼ãã¨æ¯ã¹ãã¨å¯¾å¿é¢ä¿ããããã¨æãã¾ãã
purrr
ã®å
é¨ã§ãdplyr
ã¨åæ§ã«magrittr
ãå¼ã³åºããããã¤ãæ¼ç®åãå¼ã³åºããããã«ãªãã¾ãã
#rubyã®å ´å p [2,4,6,8,10] .select{|num| num <= 8; } # 1. âã®é åãã8以ä¸ã®æ°ã ããé¸æããé åãä½ã .map{|num| num**2 } # 2. âã®é åããåè¦ç´ ãï¼ä¹ããé åãä½ã .select{|num| num >= 20 } # 3. âã®é åãã20以ä¸ã®æ°ã ããé¸æããé åãä½ã .reduce(:*) # 4. âã®é åã®å¤ãæãåããã
å®ã¯ãmap, reduceãªã©ã®é«éé¢æ°ã¨åãæ©è½ã¯ã
Rã«ãããã©ã«ãã§applyç³»ã®é¢æ°ã
Map
, Reduce
, Filter
ãªã©ã®é¢æ°ã¨ãã¦ç¨æããã¦ãã¾ãã
purrr
ã§ã¯ããã¤ãæ¼ç®åã§ã¡ã½ãããã§ã¼ã³ãä½ããããããããããã®æ©è½ãæ´çããã¦ãã¾ãã
ä¾ãã°ãããã©ã«ãã®Rã§å
ã»ã©ã®ãã®ã¨åãå¦çãæ¸ãã¨ã次ã®ããã«ãªãã¾ãã
å®éã¯applyç³»ã®é¢æ°ã使ã人ã®ã»ããå¤ãã¨æãã¾ããã
ä»è¨èªã®äººãè¦ã¦ãåãããªãã¨æãã®ã§Map
ãªã©ã®ä¸è¬çãªååã®é¢æ°ã使ãã¾ããã
Reduce( `*`, Filter( function(x) {x >= 20}, Map( function(x) {x ** 2}, Filter( function(x) {x <= 9}, c(2, 4, 6, 8, 10) ) ) ) )
ã¾ããRã®é«éé¢æ°ã¯ç¬¬ä¸å¼æ°ãé¢æ°ã®ãã®ãå¤ãã®ã§ããã¤ãæ¼ç®å%>%
ã§å¦çãç¹ããã®ã«ã¯åãã¦ãã¾ããã
ã¾ããã©ã ãå¼ãæ¸ãã®ã«ãã¡ãã¡function(){}
ã¨æ¸ãã®ã¯å°ã
é¢åã§ãã
ãã®ããpurrr
ã§ã¯ç¬¬ä¸å¼æ°ãæä½ããããã¯ãã«ã«å¤ãã£ã¦ããã
第äºå¼æ°ã«ãã«ã~ã§å§ã¾ãã©ã ãå¼(~. <= 8
ãªã©)ã使ããã¨ãã§ãã¾ãã
ãã®~ã§å§ã¾ããã®ã¯Rã®ã¢ãã«å¼ã¨ãããã®ã§ã
purrr
ã®é¢æ°ã®å
é¨ã§ãé常ã®é¢æ°ã«å±éããã¦ãã¾ãã
ã¾ããæ®éã®é¢æ°ãä¸ããã¨ããæã£ãéãã«åä½ãã¾ãã
purrr
ã®ããä¸ã¤ã®ç´ æ´ãããç¹ã¯ãé¨åé©ç¨ãããå¼æ°ããmap
ãreduce
ãªã©ã®å¼æ°ã¨ãã¦ä¸ãããããã¨ã§ãã
c('ichigo', 'aoi', 'ran') %>% purrr::map(paste, 'chan') # [[1]] # [1] "ichigo chan" # # [[2]] # [1] "aoi chan" # # [[3]] # [1] "ran chan" # ã©ã ãå¼ã使ã£ã¦æ¸ãã¨ãããªæã c('ichigo', 'aoi', 'ran') %>% purrr::map(~paste(., 'chan')) # ã¾ãããã®å ´åã¯ãã¯ãã«æ¼ç®ã§ãã§ãããã ãã©ã⦠paste(c('ichigo', 'aoi', 'ran'), 'chan')) # æ¬å½ã¯æååã®ãã¯ãã«ãã«ã³ãåºåãã«å¤æããä¾ãåºãããã£ãã®ã§ããã c('1', '2', '3') %>% purrr::reduce(paste, sep=',') # èªåã®Macç°å¢ã§ã¯é¨åé©ç¨ããã¾ããã£ã¦ããªãããã§ã # [1] "1 2 3" # ä¼ç¤¾ã®windowsç°å¢ã§ã¯æ³å®éãåããã®ã§ãã⦠# [1] "1,2,3"
ã¾ãããã®éã¯ããã¼ã¿ãã¼ã¹ã«å
¥ã£ã¦ããªãæ¥ä»ãä½æ¥ããã£ããããDBã«å
¥ã£ã¦ããªãæ¥ä»ããã£ã«ã¿ã¼ããã®ã«ä½¿ãã¾ããã
ä½è«ã§ãããfilter
ã§ã¯ãªãkeep
ã¨ããé¢æ°åã使ã£ã¦ããã®ã¯ã
æ¢ã«dplyr
ã«filter
ã¨ããé¢æ°åããããæ··ä¹±ãé¿ããããã ã¨æããã¾ãã
# ãã¼ã¿ãã¼ã¹ããåã£ã¦ããæ¥ä»ããuniqueé¢æ°ã§ä¸æã«ãã dates_in_db <- db_dataframe$date %>% unique() # 調ã¹ããæ¥ä»ã®ç¯å² # å®éã«ã¯ä¸å¹´åãããã®æ¥ä»ãé¢æ°ã§ä½ãã¾ãã target_dates <- list('20151201', '20151202', '20151203') # ãã¼ã¿ãã¼ã¹ã«åå¨ããªãæ¥ä»ããã£ã«ã¿ã¼ãã target_dates %>% purrr::keep(~!. %in% dates_in_db)
ããã«purrr
ã«ã¤ãã¦è©³ããç¥ãããæ¹ã¯ã次ã®è¨äºãèªãã¨ããã§ãããã
ãã ããå®ã¯ç¾å¨ã®purrr
ã®ãã¼ã¸ã§ã³ã¯0.2ã§ãä»å¾ãã¼ã¸ã§ã³1ã«ãªãã¾ã§ã«å¾æ¹äºææ§ãå£ããããªå¤æ´ããããã¾ãã
ã¾ããè¨äºã§åãä¸ãããã¦ããã®ãå¤ããã¼ã¸ã§ã³(0.1)ã®å ´åã注æãå¿
è¦ã§ãã
ãã ããmap
, reduce
, keep
(filteré¢æ°ã«å¯¾å¿)ãªã©ã®æ©è½ã¯ã
Pythonçã®ä»ã®è¨èªã«ãè¦ãããä¸è¬çãªãã®ãªã®ã§ãããã¾ã§å¤§ããªå¤æ´ã¯ãªãã¨å人çã«ã¯æã£ã¦ãã¾ãã
R6ã«ãããªãã¸ã§ã¯ãæåããã°ã©ãã³ã°
RubyãPythonã使ã£ã¦ããã¨ãå°ããªã³ã¼ããã ãã ã大ãããªã£ã¦ããã¨ãã ã¯ã©ã¹ãä½ã£ã¦ãã¸ãã¯ãæ´çããããªãã¨æãã¾ãã
Rãä¸å¿ãªãã¸ã§ã¯ãæåè¨èªã§ãR3ã¯ã©ã¹ãR4ã¯ã©ã¹ãªã©ã®æ©è½ãããã®ã§ããã ä»ã®è¨èªã®æ¦å¿µã¨ã¯ããªãç°ãªããã®ã§ãæ£ç´ç§ãç解ã§ãã¦ãã¾ããâ¦ã ä»è¨èªã®ããã°ã©ãã¼ã®æ³åãããæ®éã®ããªãã¸ã§ã¯ãæåã使ãã«ã¯ãR6ã¨ããããã±ã¼ã¸ã使ãã¾ãããã
R6ã©ã¤ãã©ãªã使ã£ã¦ãChatworkã®APIãå©ãã¯ã©ã¹ãä½ã£ã¦ã¿ãäºããã£ãã®ã§ããã¡ããä¾ã«åºãã¦ã¿ã¾ãã
R6::R6Class
ã®é¢æ°ã®ç¬¬ä¸å¼æ°ã«ã¯ã©ã¹åã第äºå¼æ°ã«publicã®å¤æ°ã¨é¢æ°ã®ãªã¹ãã第ä¸å¼æ°ã«privateã®å¤æ°ã¨é¢æ°ã®ãªã¹ãã渡ãã¾ãã
ããã§ã¯è§¦ãã¦ã¾ããããç¶æ¿ãã§ããããã§ãã
ï¼ç¶æ¿ãå¿
è¦ãªã»ã©å¤§ããããã°ã©ã ãæ¸ããªããä»ã®è¨èªã«ç§»æ¤ããã»ããããæ°ããã¾ããâ¦ï¼
library(R6) library(utils) library(RCurl) library(rjson) api <- R6::R6Class("api", public = list( initialize = function(access_token) { private$access_token <- access_token }, # room_idã®é¨å±ã«ã¡ãã»ã¼ã¸ãæ稿ãã post_messages = function(message, room_id) { result <- RCurl::postForm( uri = private$make_uri('rooms', room_id, 'messages'), .opts = list( httpheader = private$make_header(), postfields = private$make_body(message) ) ) return( rjson::fromJSON(result) ) }, private = list( access_token = NA, url = 'https://api.chatwork.com/v1', make_header = function() { # ãããã¼ãä½æãã }, # Chatworkã®APIã®ç¨®é¡ã«ãã£ã¦URIã®é層ã®é·ããéãã®ã§ãå¯å¤é·å¼æ°ã§æååã渡ãã¦"/"åºåãã«å¤æããé¢æ°ãç¨æ make_uri = function(...) { # URIãä½æãã }, make_body = function(message) { # BODYãä½æãã } ) ) # ã¤ã³ã¹ã¿ã³ã¹ãä½ã client <- api$new(access_token = '82*************') # Chatworkã«ã¡ãã»ã¼ã¸ãæ稿ãã client$post_messages(message = 'Hello Chatwork!', room_id = 41******) # $message_id # [1] 995512653
é¢æ°ã®å¼æ°ã§ç¡çããã¯ã©ã¹ãä½ãæãããJavaScriptã®ã©ã¤ãã©ãªã£ã½ãæ°ããã¾ãã å ã ã¯ã©ã¹ãã¼ã¹ã®ãªãã¸ã§ã¯ãæåãããªãè¨èªããç¡çããã¯ã©ã¹ãå®ç¾ããã¨ãããªæãã«ãªããã ã¨æãã¾ãã
ï¼Rã®ãªãã¸ã§ã¯ãæåãå«ãã¦ï¼è©³ãã話ã¯ãã¡ãã®çºè¡¨è³æãè¦ãã¨è¯ãã§ãã
testthatã«ããåä½ãã¹ã
åæã®Rããã°ã©ãã¼ã«ããRubyã®RSpecã¿ãããªãã®ãªãã®ï¼ãã¨è³ªåããã¨ãããtestthatã¨ããã©ã¤ãã©ãªãæãã¦ãããã¾ããã
ããã±ã¼ã¸ã®ä¸ã«éçºæã«ã¯ããå©ç¨ããã¦ããããã§ãã GitHubã§å ¬éããã¦ããRã®ã©ã¤ãã©ãªã®ã³ã¼ããèªãã¨ãtestthatã®ãã¹ãã³ã¼ããããè¦ããã¾ãã
Hacking is believing: R でいまどきなパッケージ開発 (devtools, testthat, roxygen2)
ç§ã¯ãã¡ãã¨ä½¿ã£ããã¨ããªãã®ã§ãã以ä¸ã®ç´¹ä»ã¯ã§ãã¾ããã R6ã¨çµã¿åãããã¯ã©ã¹ã®ä¸ã®privateé¢æ°ããã¹ãã§ããããªã©æ°ã«ãªãã®ã§ããâ¦ã
shinyãªã©ã使ã£ãã¬ãã¼ãã®å ±æ
ç°¡åãªãã¼ã¿ã®å¯è¦åãã¼ã«ãã社å ãã¼ã«ããããªãç°¡åã«ä½ãã¾ãã ãµã¼ãã¼ä¸ã«Rãã¤ã³ã¹ãã¼ã«ããshinyã¨ããã©ã¤ãã©ãªã使ãã°ãRã®è§£æçµæããã©ã¦ã¶ã§è¡¨ç¤ºã»URLã§å ±æã§ããããã«ãªãã¾ãã
åæã®ããã»ã¹ã§ãã¡ãã¡æä½æ¥ããããªããåæçµæããã®ã¾ã¾å ±æãããã¨ãããæ æ°ãªRããã°ã©ãã¼ãããã©ã¤ãã©ãªã ã¨æãã¾ãã
æè¿ã¯leafletã¨ããå°å³ã®ã©ã¤ãã©ãªã¨çµã¿åãããã®ãæµè¡ã£ã¦ããããã§ããã