TokyoR 68 LT
ViewとEnvironment Paneでデータの構造も見てみよう

niszet

2018/03/03

自己紹介

こんな人です

名前:niszet*
Rおじさん歴:2年目
趣味:R、楽器(お休み中)、ラズパイなど
self

普段は回路設計などしています

Rでデータを視る

Rでデータを視る…

  • 変数名をコンソールに書いてEnter
    • irisとか。コンソール上に全部表示されてしまう…
  • コンソール上で要約を視る
    • summary(), fivenum(), IRQ(), skimr::skim(), …
  • データの関係をプロットして視る
    • plot(), hist(), ggplot2, plotly, …
  • データ(オブジェクト)の構造を視る
    • str(), View(), class(), attributes(), typeof(), …

データの構造とは?

  • vector
    • 要素はすべて同じ型。2次元なら matrix, 多次元なら array
  • data.frame
    • 列ごとに同じ型。行数は全列で同じ
    • これの拡張版?に tibble がある
  • list
    • 異なる型と長さのベクトルを要素に持つことが出来る
    • list を要素に持つことも出来、複雑な構造がつくれる
  • S3, S4, RC(R5) などのクラス
    • S3はlist構造にclass属性を持たせたもの
  • 環境(environment)

…など。今回はlistに注目。

str()でデータの構造を確認する

  • str()の出力はコンソールに直接表示。
    • 変数に代入できないためhead()も出来ない。
    • 簡単な構造の変数ならこれでも十分確認できる。
## List of 4
##  $ a: int [1:10] 1 2 3 4 5 6 7 8 9 10
##  $ b: num 2
##  $ c: chr "b"
##  $ d: cplx 1+3i
## NULL

複雑なlist構造はstr()では見きれない

  • 例:repurrrsiveパッケージ
    Examples of Recursive Lists and Nested or Split Data Frames.
## List of 6
##  $ :List of 30
##   ..$ login              : chr "gaborcsardi"
##   ..$ id                 : int 660288
##   ..$ avatar_url         : chr "https://avatars.githubusercontent.com/u/660288?v=3"
##   ..$ gravatar_id        : chr ""
##   ..$ url                : chr "https://api.github.com/users/gaborcsardi"
##   ..$ html_url           : chr "https://github.com/gaborcsardi"
##   ..$ followers_url      : chr "https://api.github.com/users/gaborcsardi/followers"
##   ..$ following_url      : chr "https://api.github.com/users/gaborcsardi/following{/other_user}"
##   ..$ gists_url          : chr "https://api.github.com/users/gaborcsardi/gists{/gist_id}"
##   ..$ starred_url        : chr "https://api.github.com/users/gaborcsardi/starred{/owner}{/repo}"
##   ..$ subscriptions_url  : chr "https://api.github.com/users/gaborcsardi/subscriptions"
##   ..$ organizations_url  : chr "https://api.github.com/users/gaborcsardi/orgs"
##   ..$ repos_url          : chr "https://api.github.com/users/gaborcsardi/repos"
##   ..$ events_url         : chr "https://api.github.com/users/gaborcsardi/events{/privacy}"
##   ..$ received_events_url: chr "https://api.github.com/users/gaborcsardi/received_events"
##   ..$ type               : chr "User"
##   ..$ site_admin         : logi FALSE
##   ..$ name               : chr "Gabor Csardi"
##   ..$ company            : chr "Mango Solutions, @MangoTheCat "
##   ..$ blog               : chr "http://gaborcsardi.org"
##   ..$ location           : chr "Chippenham, UK"
##   ..$ email              : chr "csardi.gabor@gmail.com"
##   ..$ hireable           : NULL
##   ..$ bio                : NULL
##   ..$ public_repos       : int 52
##   ..$ public_gists       : int 6
##   ..$ followers          : int 303
##   ..$ following          : int 22
##   ..$ created_at         : chr "2011-03-09T17:29:25Z"
##   ..$ updated_at         : chr "2016-10-11T11:05:06Z"
##  $ :List of 30
##   ..$ login              : chr "jennybc"
##   ..$ id                 : int 599454
##   ..$ avatar_url         : chr "https://avatars.githubusercontent.com/u/599454?v=3"
##   ..$ gravatar_id        : chr ""
##   ..$ url                : chr "https://api.github.com/users/jennybc"
##   ..$ html_url           : chr "https://github.com/jennybc"
##   ..$ followers_url      : chr "https://api.github.com/users/jennybc/followers"
##   ..$ following_url      : chr "https://api.github.com/users/jennybc/following{/other_user}"
##   ..$ gists_url          : chr "https://api.github.com/users/jennybc/gists{/gist_id}"
##   ..$ starred_url        : chr "https://api.github.com/users/jennybc/starred{/owner}{/repo}"
##   ..$ subscriptions_url  : chr "https://api.github.com/users/jennybc/subscriptions"
##   ..$ organizations_url  : chr "https://api.github.com/users/jennybc/orgs"
##   ..$ repos_url          : chr "https://api.github.com/users/jennybc/repos"
##   ..$ events_url         : chr "https://api.github.com/users/jennybc/events{/privacy}"
##   ..$ received_events_url: chr "https://api.github.com/users/jennybc/received_events"
##   ..$ type               : chr "User"
##   ..$ site_admin         : logi FALSE
##   ..$ name               : chr "Jennifer (Jenny) Bryan"
##   ..$ company            : chr "University of British Columbia"
##   ..$ blog               : chr "https://twitter.com/JennyBryan"
##   ..$ location           : chr "Vancouver, BC, Canada"
##   ..$ email              : NULL
##   ..$ hireable           : NULL
##   ..$ bio                : chr "prof at UBC, humane #rstats, statistics, teach @STAT545-UBC, leadership of @rOpenSci, @rsheets, academic director @ubc-mds"
##   ..$ public_repos       : int 168
##   ..$ public_gists       : int 54
##   ..$ followers          : int 780
##   ..$ following          : int 34
##   ..$ created_at         : chr "2011-02-03T22:37:41Z"
##   ..$ updated_at         : chr "2016-10-24T07:20:26Z"
##  $ :List of 30
##   ..$ login              : chr "jtleek"
##   ..$ id                 : int 1571674
##   ..$ avatar_url         : chr "https://avatars.githubusercontent.com/u/1571674?v=3"
##   ..$ gravatar_id        : chr ""
##   ..$ url                : chr "https://api.github.com/users/jtleek"
##   ..$ html_url           : chr "https://github.com/jtleek"
##   ..$ followers_url      : chr "https://api.github.com/users/jtleek/followers"
##   ..$ following_url      : chr "https://api.github.com/users/jtleek/following{/other_user}"
##   ..$ gists_url          : chr "https://api.github.com/users/jtleek/gists{/gist_id}"
##   ..$ starred_url        : chr "https://api.github.com/users/jtleek/starred{/owner}{/repo}"
##   ..$ subscriptions_url  : chr "https://api.github.com/users/jtleek/subscriptions"
##   ..$ organizations_url  : chr "https://api.github.com/users/jtleek/orgs"
##   ..$ repos_url          : chr "https://api.github.com/users/jtleek/repos"
##   ..$ events_url         : chr "https://api.github.com/users/jtleek/events{/privacy}"
##   ..$ received_events_url: chr "https://api.github.com/users/jtleek/received_events"
##   ..$ type               : chr "User"
##   ..$ site_admin         : logi FALSE
##   ..$ name               : chr "Jeff L."
##   ..$ company            : NULL
##   ..$ blog               : chr "http://biostat.jhsph.edu/~jleek/"
##   ..$ location           : chr "Baltimore,MD"
##   ..$ email              : NULL
##   ..$ hireable           : NULL
##   ..$ bio                : NULL
##   ..$ public_repos       : int 67
##   ..$ public_gists       : int 12
##   ..$ followers          : int 3958
##   ..$ following          : int 6
##   ..$ created_at         : chr "2012-03-24T18:16:43Z"
##   ..$ updated_at         : chr "2016-10-05T13:03:04Z"
##  $ :List of 30
##   ..$ login              : chr "juliasilge"
##   ..$ id                 : int 12505835
##   ..$ avatar_url         : chr "https://avatars.githubusercontent.com/u/12505835?v=3"
##   ..$ gravatar_id        : chr ""
##   ..$ url                : chr "https://api.github.com/users/juliasilge"
##   ..$ html_url           : chr "https://github.com/juliasilge"
##   ..$ followers_url      : chr "https://api.github.com/users/juliasilge/followers"
##   ..$ following_url      : chr "https://api.github.com/users/juliasilge/following{/other_user}"
##   ..$ gists_url          : chr "https://api.github.com/users/juliasilge/gists{/gist_id}"
##   ..$ starred_url        : chr "https://api.github.com/users/juliasilge/starred{/owner}{/repo}"
##   ..$ subscriptions_url  : chr "https://api.github.com/users/juliasilge/subscriptions"
##   ..$ organizations_url  : chr "https://api.github.com/users/juliasilge/orgs"
##   ..$ repos_url          : chr "https://api.github.com/users/juliasilge/repos"
##   ..$ events_url         : chr "https://api.github.com/users/juliasilge/events{/privacy}"
##   ..$ received_events_url: chr "https://api.github.com/users/juliasilge/received_events"
##   ..$ type               : chr "User"
##   ..$ site_admin         : logi FALSE
##   ..$ name               : chr "Julia Silge"
##   ..$ company            : NULL
##   ..$ blog               : chr "juliasilge.com"
##   ..$ location           : chr "Salt Lake City, UT"
##   ..$ email              : NULL
##   ..$ hireable           : NULL
##   ..$ bio                : chr "Data science and visualization, #rstats, parenthood, reading, food/wine/coffee, #NASADatanauts."
##   ..$ public_repos       : int 26
##   ..$ public_gists       : int 4
##   ..$ followers          : int 115
##   ..$ following          : int 10
##   ..$ created_at         : chr "2015-05-19T02:51:23Z"
##   ..$ updated_at         : chr "2016-10-20T10:41:43Z"
##  $ :List of 30
##   ..$ login              : chr "leeper"
##   ..$ id                 : int 3505428
##   ..$ avatar_url         : chr "https://avatars.githubusercontent.com/u/3505428?v=3"
##   ..$ gravatar_id        : chr ""
##   ..$ url                : chr "https://api.github.com/users/leeper"
##   ..$ html_url           : chr "https://github.com/leeper"
##   ..$ followers_url      : chr "https://api.github.com/users/leeper/followers"
##   ..$ following_url      : chr "https://api.github.com/users/leeper/following{/other_user}"
##   ..$ gists_url          : chr "https://api.github.com/users/leeper/gists{/gist_id}"
##   ..$ starred_url        : chr "https://api.github.com/users/leeper/starred{/owner}{/repo}"
##   ..$ subscriptions_url  : chr "https://api.github.com/users/leeper/subscriptions"
##   ..$ organizations_url  : chr "https://api.github.com/users/leeper/orgs"
##   ..$ repos_url          : chr "https://api.github.com/users/leeper/repos"
##   ..$ events_url         : chr "https://api.github.com/users/leeper/events{/privacy}"
##   ..$ received_events_url: chr "https://api.github.com/users/leeper/received_events"
##   ..$ type               : chr "User"
##   ..$ site_admin         : logi FALSE
##   ..$ name               : chr "Thomas J. Leeper"
##   ..$ company            : chr "London School of Economics"
##   ..$ blog               : chr "http://www.thomasleeper.com"
##   ..$ location           : chr "London, United Kingdom"
##   ..$ email              : NULL
##   ..$ hireable           : logi TRUE
##   ..$ bio                : chr "Political scientist and R hacker. Interested in open science, public opinion research, surveys, experiments, cr"| __truncated__
##   ..$ public_repos       : int 99
##   ..$ public_gists       : int 46
##   ..$ followers          : int 213
##   ..$ following          : int 230
##   ..$ created_at         : chr "2013-02-07T21:07:00Z"
##   ..$ updated_at         : chr "2016-10-20T10:36:25Z"
##  $ :List of 30
##   ..$ login              : chr "masalmon"
##   ..$ id                 : int 8360597
##   ..$ avatar_url         : chr "https://avatars.githubusercontent.com/u/8360597?v=3"
##   ..$ gravatar_id        : chr ""
##   ..$ url                : chr "https://api.github.com/users/masalmon"
##   ..$ html_url           : chr "https://github.com/masalmon"
##   ..$ followers_url      : chr "https://api.github.com/users/masalmon/followers"
##   ..$ following_url      : chr "https://api.github.com/users/masalmon/following{/other_user}"
##   ..$ gists_url          : chr "https://api.github.com/users/masalmon/gists{/gist_id}"
##   ..$ starred_url        : chr "https://api.github.com/users/masalmon/starred{/owner}{/repo}"
##   ..$ subscriptions_url  : chr "https://api.github.com/users/masalmon/subscriptions"
##   ..$ organizations_url  : chr "https://api.github.com/users/masalmon/orgs"
##   ..$ repos_url          : chr "https://api.github.com/users/masalmon/repos"
##   ..$ events_url         : chr "https://api.github.com/users/masalmon/events{/privacy}"
##   ..$ received_events_url: chr "https://api.github.com/users/masalmon/received_events"
##   ..$ type               : chr "User"
##   ..$ site_admin         : logi FALSE
##   ..$ name               : chr "Maelle Salmon"
##   ..$ company            : chr "ISGlobal"
##   ..$ blog               : chr "https://masalmon.github.io/"
##   ..$ location           : chr "Barcelona, Spain"
##   ..$ email              : NULL
##   ..$ hireable           : NULL
##   ..$ bio                : chr "I use R for getting, preparing, visualizing and analyzing data mostly about air quality & public health."
##   ..$ public_repos       : int 31
##   ..$ public_gists       : int 0
##   ..$ followers          : int 34
##   ..$ following          : int 38
##   ..$ created_at         : chr "2014-08-05T08:10:04Z"
##   ..$ updated_at         : chr "2016-10-24T09:54:57Z"

RStudio IDEの
Environment Paneを使う

List での表示

  • (>)のアイコンで折り畳み/展開できるので必要な情報だけ詳しく見れる

List での表示(展開した様子)

  • 展開するとstr()と同じ表示が得られる
    • ここならば表示してもコンソールが汚れない!

Grid での表示

  • Type, Length, Size(メモリ使用量), Valueを確認できる
    • いずれも簡易的な表示であることに注意
  • 箒アイコンで個別に変数を消せる点は便利
    • Listは箒で全部消える。慈悲はない

RStudio IDE 上で View() を使ってデータの階層構造を見る

RStudio IDE v1.1の新機能、
Object Explorer

  • RStudio v1.1 – What’s New

https://kevinushey-2018-rstudio-conf.netlify.com/slides.html

View(repurrrsive::gh_users)

  • 階層構造がわかりやすい
    • (>)のアイコンで折りたためるので必要な情報だけに注目できる
  • Type(型、クラス)やValue(値)に詳細な情報が載っている

ggplot2パッケージの場合

kazutanさんの資料をView()を使い手を動かしながら
視ていくとさらにわかりやすい…!

https://kazutan.github.io/HijiyamaR6/intro_ggplot2.html

ggplot()にdataを指定した場合

とりあえずいい感じにプロットしてみる

g_def_gpoint_null <- ggplot(data = iris, mapping = 
           aes(x = Sepal.Length, y = Sepal.Width)) + geom_point()
g_def_gpoint_null

View(g_def_gpoint_null)

  • 一番上の階層の datairis のデータが入っていることがわかる。
    • geom_point()に引数を与えていないので、layersdataは空

geom_point()にdataを指定した場合

g_gpoint_iris <- ggplot() + geom_point(data = iris, 
             mapping = aes(x = Sepal.Length, y = Sepal.Width))
g_gpoint_iris

View(g_gpoint_iris)

  • geom_point()に引数を指定したので、top階層のdataは空
    • irisのデータはlayersの中に入っている
    • plotした図が同じように見えても持っているデータの構造は異なることがわかる

注意点

細かいことですが…。

  • タブに変数名と異なる名前を与えるとアイコンを押して値を抜き出す方法は使えない
    • View(iris, "hoge") だとhogeという変数から値を取ろうとしてしまうため
  • list構造のデータは元データが更新されてもView()で表示させたタブの表示が更新されない。
    • 同じ変数をView()で表示するときは先にタブを閉じておくこと
  • data.frameやtibbleはas.list()などで変換してからView()で表示すれば今回の方法が使える
  • このViewの挙動はRStudio IDEで上書きしているため、utils::View()のヘルプを見てもわからないので注意。

まとめ

データ構造も可視化するぞ…!

  • 基本は str()
  • RStudio IDE の Environment Pane の活用
  • View()
    • 特に複雑な構造をしている際に、目視で構造を確認できる
    • 折りたためるので情報量を制限できる
    • 詳細な情報が見れる
    • 特定の要素を選択して取り出すことができる
    • zeallotでデータを抜き出す際の確認などに。

詳細はブログで

  • 今年は40件以上投稿している(R以外の内容も含む)
  • 今回の内容の詳細をそれぞれまとめてあります。

Enjoy!!

self