BeginnerSession1_69th_TokyoR

 BeginnerSession1_69th_TokyoR

第69回TokyoR初心者セッション1の資料です。

8284465a94bbdf1ea82cf1a67d55f447?s=128

kilometer

April 21, 2018
Tweet

Transcript

  1. 69th Tokyo.R @kilometer BeginneR Session 1 -- Data Import /

    Export -- 2018.04.21 at Roppongi
  2. Who!?

  3. Who!? 名前: 三村 @kilometer 職業: ポスドク (こうがくはくし) 専門: 行動神経科学(霊長類) 脳イメージング

    医療システム工学 R歴: ~ 10年ぐらい 流行: ラム肉
  4. BeginneR Session

  5. BeginneR

  6. BeginneR Advanced Hoxo_m If I have seen further it is

    by standing on the sholders of Giants. -- Sir Isaac Newton, 1676
  7. BeginneR

  8. Before After BeginneR Session BeginneR BeginneR

  9. Input Output Today’s talk ? ? Table data Image data

    Other formats Introduction Summary
  10. を、始めたいんだけど。 私もそろそろ ぱそこん できるんでしょ?教えてよ。 ぱそこん いいけど、 で何がしたいの? ぱそこん って何でもできるんでしょ? でも、どうしたらいいか分かんないんだ。

    一昔前
  11. を、始めたいんだけど。 私もそろそろ じんこう ちのー できるんでしょ?教えてよ。 いいけど、 で何がしたいの? って何でもできるんでしょ? でも、どうしたらいいか分かんないんだ。 最近

    じんこう ちのー じんこう ちのー
  12. を、始めたいんだけど。 私もそろそろ できるんでしょ?教えてよ。 いいけど、 で何がしたいの? って何でもできるんでしょ? でも、どうしたらいいか分かんないんだ。

  13. を、始めたいんだけど。 私もそろそろ できるんでしょ?教えてよ。 いいけど、 で何がしたいの? って何でもできるんでしょ? でも、どうしたらいいか分かんないんだ。

  14. Input Output Do NOT start from here

  15. Input Output Do NOT start from here Whatever …

  16. Input Output Do NOT start from here Whatever … 1

    2 3
  17. Input Output Today’s talk ? ?

  18. Integrated Development Environment RStudio https://www.rstudio.com/

  19. Integrated Development Environment RStudio https://www.rstudio.com/

  20. RStudio

  21. Projects RStudio

  22. RStudio > Project 一説には2147483647個存在するとも言われるRStudioの利 点のなかでも、 ・Rなどのソースファイルをタブで並べて表示できる ・そのタブの順番を保持できる ・タブの内容をファイルを保存せずにRStudioを終了して しまっても、編集途中の内容を保持してくれている 等が全国2147483647人のRユーザーのQOLを大いに向上さ

    せたのは、おそらく異論がないことと思われます。 34UVEJPͬͯͳΜͩʁ 1SPKFDUͬͯͳΜͩ !XBLVUFLB https://qiita.com/wakuteka/items/9599bb0a8985d98928d7
  23. File > New Project… > New Directory > New Project

    hogehoge
  24. hogehoge ~/Documents/R hogehoge.Rproj .Rproj.user Project Root Directory Double click!! .RData

    .Rhistory Auto saved project information Open project New!!
  25. ~/Documents/R project1 project2 project3 project4

  26. Input Output Today’s talk ? ? Table data Image data

    Other formats Summary Introduction
  27. Table format data Table data Image data Other formats Introduction

    Summary
  28. x <- read.table("import.txt") Import Table format data object assignment operator

    file path function
  29. x <- read.table("import.txt") Import Table format data object assignment operator

    file path function
  30. ~/Documents/ import.txt Target!! data x <- read.table("~/Documents/data/import.txt") setwd("~/Documents/data/") x <-

    read.table("import.txt") 1. set working directory 2. then use relative path Method②: use absolute path Method ① Import Table format data
  31. x <- read.table("~/Documents/data/import.txt") setwd("~/Documents/data/") x <- read.table("import.txt") Method②: use absolute

    path Method ①: setwd – relative path # working directory wd <- c("~/Documents/data/") # make full path: bind characters path <- paste0(wd, "import.txt") # import data x <- read.table(path) or
  32. prj ~/Documents/R prj.Rproj import.txt Default working directory (project root) Target!!

    x <- read.table("import.txt") can use relative path without setwd
  33. x <- read.table(file) Import Table format data

  34. x <- read.table(file) Import Table format data you

  35. Table format data Comma-separated values (.csv) Tab-separated values (.tsv) Space-SV

    Semicolon-SV … Delimiter-separated values
  36. Table format data x = np.genfromtxt("hoge.csv", delimiter=",") x <- read.table("hoge.csv",

    sep=",") Optional argument(s)
  37. ?read.table Usage read.table(file, header = FALSE, sep = "", quote

    = "∖", dec = ".", numerals = c("allow.loss", "warn.loss", "no.loss"), row.names, col.names, as.is = !stringsAsFactors, na.strings = "NA", colClasses = NA, nrows = -1, skip = 0, check.names = TRUE, fill = !blank.lines.skip, strip.white = FALSE, blank.lines.skip = TRUE, comment.char = "#", allowEscapes = FALSE, flush = FALSE, stringsAsFactors = default.stringsAsFactors(), fileEncoding = "", encoding = "unknown", text, skipNul = FALSE) Import Table format data
  38. ?read.table Usage read.table(file, header = FALSE, sep = "", quote

    = "∖", dec = ".", numerals = c("allow.loss", "warn.loss", "no.loss"), row.names, col.names, as.is = !stringsAsFactors, na.strings = "NA", colClasses = NA, nrows = -1, skip = 0, check.names = TRUE, fill = !blank.lines.skip, strip.white = FALSE, blank.lines.skip = TRUE, comment.char = "#", allowEscapes = FALSE, flush = FALSE, stringsAsFactors = default.stringsAsFactors(), fileEncoding = "", encoding = "unknown", text, skipNul = FALSE) Import Table format data You must specify If not specified, applied this default value Optional argument(s) 引数 "file" がありませんし、省略時既定値もありません If not,
  39. ?read.table Usage read.table(file, header = FALSE, sep = "", quote

    = "∖", dec = ".", numerals = c("allow.loss", "warn.loss", "no.loss"), row.names, col.names, as.is = !stringsAsFactors, na.strings = "NA", colClasses = NA, nrows = -1, skip = 0, check.names = TRUE, fill = !blank.lines.skip, strip.white = FALSE, blank.lines.skip = TRUE, comment.char = "#", allowEscapes = FALSE, flush = FALSE, stringsAsFactors = default.stringsAsFactors(), fileEncoding = "", encoding = "unknown", text, skipNul = FALSE) Import Table format data
  40. ?read.table Usage read.table(file, skip = 0, header = FALSE, nrows

    = -1, row.names, fileEncoding = "") Import Table format data
  41. cf. R-Tips: 40. ファイルからデータを読み込む http://cse.naro.affrc.go.jp/takezawa/r-tips/r/40.html > x sex height weight

    1 F 158 51 2 F 162 55 3 M 177 72 4 M 173 57 5 M 166 64 Import Table format data
  42. x <- read.table( "import.txt", skip = 1, header = TRUE,

    row.names = "data", nrows = 5, fileEncoding = "UTF8") cf. R-Tips: 40. ファイルからデータを読み込む http://cse.naro.affrc.go.jp/takezawa/r-tips/r/40.html Import Table format data
  43. x <- read.table( "import.txt", skip = 1, header = TRUE,

    row.names = "data", nrows = 5, fileEncoding = "UTF8") cf. R-Tips: 40. ファイルからデータを読み込む http://cse.naro.affrc.go.jp/takezawa/r-tips/r/40.html Import Table format data
  44. x <- read.table( "import.txt", skip = 1, header = TRUE,

    row.names = "data", nrows = 5, fileEncoding = "UTF8") cf. R-Tips: 40. ファイルからデータを読み込む http://cse.naro.affrc.go.jp/takezawa/r-tips/r/40.html Import Table format data
  45. x <- read.table( "import.txt", skip = 1, header = TRUE,

    row.names = "data", nrows = 5, fileEncoding = "UTF8") cf. R-Tips: 40. ファイルからデータを読み込む http://cse.naro.affrc.go.jp/takezawa/r-tips/r/40.html Import Table format data
  46. x <- read.table( "import.txt", skip = 1, header = TRUE,

    row.names = "data", nrows = 5, fileEncoding = "UTF8") cf. R-Tips: 40. ファイルからデータを読み込む http://cse.naro.affrc.go.jp/takezawa/r-tips/r/40.html Import Table format data # 先頭_行は読み込まない # カラム名の行が存在する # 行名のカラム名/番号を指定 # データ部分の行数
  47. x <- read.table( "import.txt", skip = 1, header = TRUE,

    row.names = "data", nrows = 5, fileEncoding = "UTF8") cf. R-Tips: 40. ファイルからデータを読み込む http://cse.naro.affrc.go.jp/takezawa/r-tips/r/40.html Import Table format data # 先頭_行は読み込まない # カラム名の行が存在する # 行名のカラム名/番号を指定 # データ部分の行数
  48. 第67回 初心者セッション1 @y__mattu https://github.com/ymattu/TokyoR67 Import Table format data

  49. x <- read.table("import.csv", header = T, sep = "," quote

    = "∖", dec = ".") x <- read.csv("import.csv") Import Table format data Wrap
  50. x <- read.table(file) Import Table format data you Wrappers

  51. x <- read.table(file) Import Table format data you

  52. Package … {utils} Package {base} Package

  53. Package Package Package Package Package Package Package Package Package Package

    Package Package Package Package Package
  54. packages you

  55. cf. http://rpkg.gepuro.net/ cf. https://cran.r-project.org/web/packages/ at 2018.02.20

  56. Package Default pkgs Package Package Package CRAN Guithub Package Package

    Package Package Package Package Package Package install.packages githubinstall
  57. utils::read.table(file) Import Table format data Default installed pkg (optional) Function

    data.table::fread(file) Install REQUIRED
  58. Import Table format data List of installed packages

  59. Import Table format data data.table::fread (file) Installation (from CRAN) install.packages("data.table")

    Usage library("data.table") fread (file) or
  60. Import Table format data library("data.table") > sessionInfo() ... other attached

    packages: [1] data.table_1.10.4-3 Global environment # Attach {data.table}
  61. Import Table format data library("data.table") fread (file, data.table = FALSE)

    Fast!!! Flexible!!! Need installation Should specify: data.table =FALSE Automatic!!! Simple!!!
  62. write.table(x, "export.txt") write.csv(x, "export.csv") object file path Export Table format

    data Wrapper
  63. Input Output Today’s talk ? ? Table data Image data

    Other formats Summary Introduction
  64. Image data Table data Image data Other formats Summary Introduction

  65. Export image data ?

  66. Export image data Use Graphic Device ready graphic device close

    graphic device draw graph(s) plotting functions ・High-level ・Low-level ・Interactive Basic Graphics in R http://bcb.dfci.harvard.edu/~aedin/courses/BiocDec2011/2.Plotting.pdf
  67. Export image data Use Graphic Device ready close draw png("hoge.png")

    plot(iris) dev.off() High-level plotting function
  68. hogehoge ~/Documents/R hogehoge.Rproj hoge.png New!!

  69. Export image data Graphic Devices in {grDevices} > help(device) pdf

    png jpeg tiff bmp ... Write PDF graphics commands to a file PNG bitmap device JPEG bitmap device TIFF bitmap device BMP bitmap device
  70. Export image data Graphic Devices in {grDevices} pdf png jpeg

    tiff bmp svg cairo_pdf cairo_ps Scalable vector graphics Cairographics based PDF Cairographics based PostScript
  71. Export plot ready graphic device close graphic device draw graph(s)

    Image data processing Import & Export image x <- read***( ) write***(x, ) processing
  72. png::readPNG Import image data tiff::readTIFF jpeg::readJPEG bmp::read.bmp

  73. png::readPNG Import image data tiff::readTIFF jpeg::readJPEG bmp::read.bmp

  74. png::readPNG Import image data tiff::readTIFF jpeg::readJPEG bmp::read.bmp EBImage::readImage magick::image_read imager::load.image

    OpenImageR::readImage biOps::readJpeg
  75. png::readPNG Import image data tiff::readTIFF jpeg::readJPEG bmp::read.bmp EBImage::readImage magick::image_read imager::load.image

    OpenImageR::readImage biOps::readJpeg
  76. Import image data EBImage::readImage magick::image_read Export image data EBImage::writeImage #

    not CRAN pkg magick::image_write # low googlability
  77. Import image data EBImage::readImage magick::image_read Export image data EBImage::writeImage #

    not CRAN pkg magick::image_write # low googlability
  78. None
  79. None
  80. None
  81. Salvador Dalí 1904-1989 profile of time wikipedia

  82. http://seekr.jp/

  83. None
  84. None
  85. None
  86. Input Output Today’s talk ? ? Table data Image data

    Other formats Summary Introduction
  87. Table data Image data Other formats Other formats Summary Introduction

  88. .xls or .xlsx readxl::read_excel( file, GJMFQBUI sheet = 1) TIFFUOBNFPS/P

  89. .json jsonlite::read_json(file) jsonlite::write_json(file) Wrap from_json(file, simplifyVector =T) Wrap to_json(obj, file,

    simplifyVector =T)
  90. tuneR::readMP3(file) .wav or .mp3 tuneR::readWave(file) tuneR::writeWave(object, file)

  91. .sas or .dta or .sav SAS Stata SPSS haven::read_sas("hoge.sas7bdat") haven::read_dta("hoge.dta")

    haven::read_sav("hoge.sav")
  92. Summary Table data Image data Other formats Summary Introduction

  93. Input Output Do NOT start from here Whatever … 1

    2 3
  94. Data Import / Export ? ?

  95. packages you

  96. Data Import / Export ? ? {readxl} {data.table} {EBImage} {magick}

    {jsonlite} {tuneR} {haven} {grDevices} {utils} packages
  97. ~/Documents/R project1 project2 project3 project4

  98. Before After BeginneR Session BeginneR BeginneR ?

  99. BeginneR Advanced Hoxo_m If I have seen further it is

    by standing on the sholders of Giants. -- Sir Isaac Newton, 1676
  100. Enjoy!! KMT˝

  101. Bar DraDra KMT˝