2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Rのデータの型 ---初心者向け---

Posted at
tags: rBasicLearning

データの型

データ型の種類

数値型

数値型は, 以下のようなものである.
typeof(x)で, xのデータ型を確かめることができる.

# Numeric

n1 <- 15  # Double precision by default
n1
typeof(n1)

n2 <- 1.5
n2
typeof(n2)
> n1 <- 15  # Double precision by default
> n1
[1] 15
> typeof(n1)
[1] "double"
> n2 <- 1.5
> n2
[1] 1.5
> typeof(n2)
[1] "double"

文字型

文字型は, 以下のようなものである.

## Character

c1 <- "c"
c1
typeof(c1)

c2 <- "a string of text"
c2
typeof(c2)
> c1 <- "c"
> c1
[1] "c"
> typeof(c1)
[1] "character"
> 
> c2 <- "a string of text"
> c2
[1] "a string of text"
> typeof(c2)
[1] "character"

論理型

論理型は, 以下のようなものである.
簡単に言うと, "TRUE" か "FALSE"である.
"T", "F" のみでも認識してくれる.

# Logical

l1 <- TRUE
l1
typeof(l1)

l2 <- F
l2
typeof(l2)
> # Logical
> 
> l1 <- TRUE
> l1
[1] TRUE
> typeof(l1)
[1] "logical"
> 
> l2 <- F
> l2
[1] FALSE
> typeof(l2)
[1] "logical"

データの構造

ベクトル

一次元のデータ列をまとめたもの.
データ列の()の前に「鎖状に繋げる」を意味する "concatenate" の "c" をつける

## Vector ##################################################

v1 <- c(1, 2, 3, 4, 5)
v1
is.vector(v1)

v2 <- c("a", "b", "c")
v2
is.vector(v2)

v3 <- c(TRUE, TRUE, FALSE, FALSE, TRUE)
v3
is.vector(v3)
> v1 <- c(1, 2, 3, 4, 5)
> v1
[1] 1 2 3 4 5
> is.vector(v1)
[1] TRUE
> 
> v2 <- c("a", "b", "c")
> v2
[1] "a" "b" "c"
> is.vector(v2)
[1] TRUE
> 
> v3 <- c(TRUE, TRUE, FALSE, FALSE, TRUE)
> v3
[1]  TRUE  TRUE FALSE FALSE  TRUE
> is.vector(v3)
[1] TRUE

行列

列ベクトルを複数組み合わせたもの.
数字をs×t行列に収めたものとも考えられる.
nrow = x で, 行の数を指定.
指定したベクトルの要素は, 一列ごとに入れられる.
byrow = TRUEとすると, 指定したベクトルの要素は, 一行ごとに入れられる.

## Matrix ##################################################

m1 <- matrix(c(T, T, F, F, T, F), nrow = 2)
m1

m2 <- matrix(c("a", "b", 
               "c", "d"), 
               nrow = 2,
               byrow = T)
m2

m3 <- matrix(c("a", "b", 
               "c", "d"), 
             nrow = 2)
m3
> m1 <- matrix(c(T, T, F, F, T, F), nrow = 2)
> m1
     [,1]  [,2]  [,3]
[1,] TRUE FALSE  TRUE
[2,] TRUE FALSE FALSE
> 
> m2 <- matrix(c("a", "b", 
+                "c", "d"), 
+                nrow = 2,
+                byrow = T)
> m2
     [,1] [,2]
[1,] "a"  "b" 
[2,] "c"  "d" 
> 
> m3 <- matrix(c("a", "b", 
+                "c", "d"), 
+              nrow = 2)
> m3
     [,1] [,2]
[1,] "a"  "c" 
[2,] "b"  "d" 

array型

array型でも, 行列を作ることができる.
最初にデータを与え, その後に次元(行, 列, 表の数)を指定する.

## Array ###################################################

# Give data, then dimemensions (rows, columns, tables)
a1 <- array(c( 1:24), c(4, 3, 2))
a1
> # Give data, then dimemensions (rows, columns, tables)
> a1 <- array(c( 1:24), c(4, 3, 2))
> a1
, , 1

     [,1] [,2] [,3]
[1,]    1    5    9
[2,]    2    6   10
[3,]    3    7   11
[4,]    4    8   12

, , 2

     [,1] [,2] [,3]
[1,]   13   17   21
[2,]   14   18   22
[3,]   15   19   23
[4,]   16   20   24

データフレーム

データフレームは, 同じ長さのベクトルを組み合わせることができる.
cbind(ベクトル1, ベクトル2, …)で組み合わせる.
しかし, cbindで組み合わせただけでは, 全てが文字型に型変換されてしまっているため, これをデータフレーム型に変えるために, as.data.frameを使う.
データフレームは, 違う型の変数の列ベクトルを, 一列ずつ並べているだけ.

## Data frame ##############################################

# Can combine vectors of the same length

vNumeric   <- c(1, 2, 3)
vCharacter <- c("a", "b", "c")
vLogical   <- c(T, F, T)

dfa <- cbind(vNumeric, vCharacter, vLogical)
dfa  # Matrix of one data type

df <- as.data.frame(cbind(vNumeric, vCharacter, vLogical))
df  # Makes a data frame with three different data types
> ## Data frame ##############################################
> 
> # Can combine vectors of the same length
> 
> vNumeric   <- c(1, 2, 3)
> vCharacter <- c("a", "b", "c")
> vLogical   <- c(T, F, T)
> 
> dfa <- cbind(vNumeric, vCharacter, vLogical)
> dfa  # Matrix of one data type
     vNumeric vCharacter vLogical
[1,] "1"      "a"        "TRUE"  
[2,] "2"      "b"        "FALSE" 
[3,] "3"      "c"        "TRUE"  
> 
> df <- as.data.frame(cbind(vNumeric, vCharacter, vLogical))
> df  # Makes a data frame with three different data types
  vNumeric vCharacter vLogical
1        1          a     TRUE
2        2          b    FALSE
3        3          c     TRUE

list

list型によっても, 違う型のデータを組み合わせることができる.

## List ####################################################

o1 <- c(1, 2, 3)
o2 <- c("a", "b", "c", "d")
o3 <- c(T, F, T, T, F)

list1 <- list(o1, o2, o3)
list1

list2 <- list(o1, o2, o3, list1)  # Lists within lists!
list2
> list1 <- list(o1, o2, o3)
> list1
[[1]]
[1] 1 2 3

[[2]]
[1] "a" "b" "c" "d"

[[3]]
[1]  TRUE FALSE  TRUE  TRUE FALSE

> 
> list2 <- list(o1, o2, o3, list1)  # Lists within lists!
> list2
[[1]]
[1] 1 2 3

[[2]]
[1] "a" "b" "c" "d"

[[3]]
[1]  TRUE FALSE  TRUE  TRUE FALSE

[[4]]
[[4]][[1]]
[1] 1 2 3

[[4]][[2]]
[1] "a" "b" "c" "d"

[[4]][[3]]
[1]  TRUE FALSE  TRUE  TRUE FALSE

データの型の強制

自分の使いたいデータの型出ない時, それを強制することが必要になってくる.

浮動少数型から整数型への強制

## Coerce numeric to integer ###############################

(coerce2 <- 5)
typeof(coerce2)

(coerce3 <- as.integer(5))
typeof(coerce3)
> ## Coerce numeric to integer ###############################
> 
> (coerce2 <- 5)
[1] 5
> typeof(coerce2)
[1] "double"
> 
> (coerce3 <- as.integer(5))
[1] 5
> typeof(coerce3)
[1] "integer"

文字型から数値型に変換

## Coerce character to numeric #############################

(coerce4 <- c("1", "2", "3"))
typeof(coerce4)

(coerce5 <- as.numeric(c("1", "2", "3")))
typeof(coerce5)
> ## Coerce numeric to integer ###############################
> 
> (coerce2 <- 5)
[1] 5
> typeof(coerce2)
[1] "double"
> 
> (coerce3 <- as.integer(5))
[1] 5
> typeof(coerce3)
[1] "integer"
> (coerce4 <- c("1", "2", "3"))
[1] "1" "2" "3"
> typeof(coerce4)
[1] "character"
> 
> (coerce5 <- as.numeric(c("1", "2", "3")))
[1] 1 2 3
> typeof(coerce5)
[1] "double"

行列からデータフレームに変換

## Coerce matrix to data frame #############################

(coerce6 <- matrix(1:9, nrow= 3))
is.matrix(coerce6)

(coerce7 <- as.data.frame(matrix(1:9, nrow= 3)))
is.data.frame(coerce7)
> ## Coerce matrix to data frame #############################
> 
> (coerce6 <- matrix(1:9, nrow= 3))
     [,1] [,2] [,3]
[1,]    1    4    7
[2,]    2    5    8
[3,]    3    6    9
> is.matrix(coerce6)
[1] TRUE
> 
> (coerce7 <- as.data.frame(matrix(1:9, nrow= 3)))
  V1 V2 V3
1  1  4  7
2  2  5  8
3  3  6  9
> is.data.frame(coerce7)
[1] TRUE
2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?