LoginSignup
20
14

More than 5 years have passed since last update.

Rの型について理解する

Last updated at Posted at 2017-07-16

型の意味、typeof, mode, class 関数の意味がよくわかっていないので調べたメモ.

Rの型

Rで型というとき、文脈によって、意味する内容が異なる場合があるので注意が必要. 大きく分けると3つの型が使われている.

  • typeof()やmode()関数で調べることのできる, basic type
  • basic typeである、vectorに含まれる要素の, type
  • class()関数で調べることのできる, オブジェクトの属性によって表される, type

typeofとmodeの違いは後で述べる.

typeofやmodeで調べることのできるbasic type

basic type には以下のtypeがある.

  • Vectors
  • Lists
  • Language objects
  • Expression objects
  • Function objects
  • Environments
  • Pairlist objects
l <- list(1:4)
mode(l)
## [1] "list"
mode(apply)
## [1] "function"
v <- 1:10
mode(v)
## [1] "numeric"

あれ?vectorは? typeofやmodeではvectorが返ってくることはない.
vector type はvectorを構成する要素の型で返ってくるからだ.
つまり, logical, integer, double, numeric, complex, character, raw が返ってきたときは, それらを含むvectorがbasic typeということになる.
あくまで基本型はvectorであることに注意.

basic typeであるvectorに含まれる要素のtype

basic type がvector typeであるオブジェクトに対して、typeofやmodeを使うと、vectorを構成する要素の型が返ってくる. 型というとき, このvectorを構成する要素の型を指していることも多い. vectorには以下の型がある.

  • logical
  • numeric
  • integer
  • double
  • complex
  • character

numericはintegerとdoubleを合わせた型で, modeで型を調べると出てくる. typeofではnumericは出てこなくて、integerとdoubleが区別される.
S言語との互換性のためにmodeの型があるようだ.
mode()とtypeof()はnumeric以外ではほぼ同じ動作になる.

x <- (c(1:3) > 2)
x
## [1] FALSE FALSE  TRUE
typeof(x)
## [1] "logical"
mode(x)
## [1] "logical"
x <- c(1.5,3.1)
typeof(x)
## [1] "double"
mode(x)
## [1] "numeric"
x <- 1:5
typeof(x)
## [1] "integer"
mode(x)
## [1] "numeric"
typeof(1)
## [1] "double"
mode(1)
## [1] "numeric"

numericということは、1はnumericを含むvector型ということになる. の基本型にはintegerはない. vectorであることを明示的に確認するにはis.vectorを使えばよい.

is.vector(1)
## [1] TRUE
x <- c("a","b","c")
x
## [1] "a" "b" "c"
typeof(x)
## [1] "character"
mode(x)
## [1] "character"

class()関数で調べることのできる, オブジェクトのclass属性によって表されるtype

属性とはオブジェクトに付随するラベルのようなもので, そのオブジェクトの特徴を表す文字列である. オブジェクトは複数の属性を持つことができる. 属性には, name, dim, dimname, class, tsp(時系列属性) などがある.
このうちclass属性によって表される名前がclass()関数で返ってくる値である.

matrix, array, factor, data.frame などがclass属性によって表される. ただし, matrixとarrayはclass属性をもたないがそれぞれmatrixクラスとarrayクラスを持つ.
なお, 属性はattributesを用いて調べることができる.

  • data frame の例
df <- iris
attributes(df)
## $names
## [1] "Sepal.Length" "Sepal.Width"  "Petal.Length" "Petal.Width" 
## [5] "Species"     
## 
## $row.names
##   [1]   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17
##  [18]  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34
##  [35]  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51
##  [52]  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68
##  [69]  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85
##  [86]  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102
## [103] 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
## [120] 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
## [137] 137 138 139 140 141 142 143 144 145 146 147 148 149 150
## 
## $class
## [1] "data.frame"

class属性を持つことがわかる

class(df)
## [1] "data.frame"
  • factor型の例
iris.species <- df$Species
attributes(iris.species)
## $levels
## [1] "setosa"     "versicolor" "virginica" 
## 
## $class
## [1] "factor"
class(df$Species)
## [1] "factor"
  • matrixの例
m <- matrix(1:9,3)
attributes(m)
## $dim
## [1] 3 3

class属性は持たないがmatrix型になる

class(m)
## [1] "matrix"
#  1940年のバージニア州の死亡率(人口1000対)
VADeaths
##       Rural Male Rural Female Urban Male Urban Female
## 50-54       11.7          8.7       15.4          8.4
## 55-59       18.1         11.7       24.3         13.6
## 60-64       26.9         20.3       37.0         19.3
## 65-69       41.0         30.9       54.6         35.1
## 70-74       66.0         54.3       71.1         50.0
class(VADeaths)
## [1] "matrix"
mode(VADeaths)
## [1] "numeric"
typeof(VADeaths)
## [1] "double"
  • ts(時系列)の例
#airmilesは1937〜1960年のアメリカ航空会社の旅客マイル(単位:1名1マイル)
airmiles
## Time Series:
## Start = 1937 
## End = 1960 
## Frequency = 1 
##  [1]   412   480   683  1052  1385  1418  1634  2178  3362  5948  6109
## [12]  5981  6753  8003 10566 12528 14760 16769 19819 22362 25340 25343
## [23] 29269 30514
class(airmiles)
## [1] "ts"

参考にさせてもらった情報

20
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
20
14