Qiita Teams that are logged in
You are not logged in to any team

Community
Service
Qiita JobsQiita ZineQiita Blog
1
Help us understand the problem. What is going on with this article?
@Quantas

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

More than 1 year has passed since last update.

# データの型

## データ型の種類

### 数値型

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"
``````

### 論理型

"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
``````

### 行列

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
``````
1
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
1. We will deliver articles that match you
By following users and tags, you can catch up information on technical fields that you are interested in as a whole
2. you can read useful information later efficiently
By "stocking" the articles you like, you can search right away