3
2020 1 30
1 1
2 data.frame 1
2.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
3 3
3.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
3.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
3.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
4 5
4.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
5 6
5.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
6 : 7
7 8
7.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1
•
2 data.frame
row column
1
R mode Numeric
Character
1
2.1 3
173.5 66 19
166.4 58 20
168 NA 18
170.3 81 20
1:
山田太郎
西南花子
市東治子
三宅信二
リスト要素
[[1]]
Character 型
ベクトル
173.5
166.4
168
170.3
リスト要素
[[2]]
Numeric 型
ベクトル
男
女
女
男
リスト要素
[[5]]
Character 型
ベクトル
福岡県
鹿児島県
千葉県
岡山県
リスト要素
[[6]]
Character 型
ベクトル
66
58
NA
20
リスト要素
[[3]]
Numeric 型
ベクトル
19
20
18
20
リスト要素
[[4]]
Numeric 型
ベクトル
1:
R data.frame 1
data.frame 1
1 2
NA Not Available
missing values R NA
logical NA
2.1
•
•
• row column
1
2 1
R
I 2
3
•
• NA missing values
3
data.frame()
> name <- c(" ", " ", " ", " ") #
> height <- c(173.5, 166.4, 168, 170.3) #
> weight <- c(66, 58, NA, 81) # NA
> age <- c(19, 20, 18, 20) #
> personal <- data.frame(name, height, weight, age) #
> personal
name height weight age
1 173.5 66 19
2 166.4 58 20
3 168.0 NA 18
4 170.3 81 20
3.1
cbind() column bind cbind()
personal
personal personal cbind()
> =c(" ", " ", " ", " ") #
> personal <- cbind(personal, ) #
> personal
name height weight age
1 173.5 66 19
2 166.4 58 20
3 168.0 NA 18
4 170.3 81 20
I 3
3.2 3
> personal <- cbind(personal, =c(" ", " ", " ", " "))
> personal
name height weight age
1 173.5 66 19
2 166.4 58 20
3 168.0 NA 18
4 170.3 81 20
3.2
rbind() row bind
2
> extra <- data.frame( =c(" ", " "),
+ =c(169, 159),
+ =c(61, 63),
+ =c(23, 20),
+ =c(" ", " "),
+ =c(" ", " "))
> extra
1 169 61 23
2 159 63 20
rbind()
> rbind(personal, extra) #
personal
colnames()
> colnames(personal) # column names
[1] "name" "height" "weight" "age" " " " "
> colnames(personal) <- c(" ", " ", " ", " ", " ", " ")
> personal
1 173.5 66 19
2 166.4 58 20
3 168.0 NA 18
4 170.3 81 20
extra personal
I 4
3.3 3
> colnames(personal) <- colnames(extra) # extra personal
> personal
1 173.5 66 19
2 166.4 58 20
3 168.0 NA 18
4 170.3 81 20
rbind() rbind()
personal
> personal <- rbind(personal, extra)
> personal
1 173.5 66 19
2 166.4 58 20
3 168.0 NA 18
4 170.3 81 20
5 169.0 61 23
6 159.0 63 20
3.3
•
•
•
•
•
4
attributes
3
attributes()
> attributes(personal)
$names
[1] " " " " " " " " " " " "
3
I 5
4.1 3
$row.names
[1] 1 2 3 4 5 6
$class
[1] "data.frame"
personal names row.names class 3
names row.names class
colnames()
names row.names rownames()
> rownames(personal) #
[1] "1" "2" "3" "4" "5" "6"
> dim(personal) # dimension
[1] 6 6
> dim(personal)[1] #
[1] 6
> dim(personal)[2] #
[1] 6
4.1
•
• attributes()
• rownames()
•
5
personal 1 ID1 ID2 · · · ID
> personal
ID1 173.5 66 19
ID2 166.4 58 20
ID3 168.0 NA 18
ID4 170.3 81 20
ID5 169.0 61 23
ID6 159.0 63 20
I 6
5.1 3
5.1
> rownames(personal) <- c("ID1", "ID2", "ID3", "ID4", "ID5", "ID6")
> # paste()
> rownames(personal) <- paste("ID", 1:dim(personal)[1], sep="")
6 :
ID
paste()
paste()
> help(paste)
Description concatenate
Usage
Usage:
paste (..., sep = " ", collapse = NULL)
paste0(..., collapse = NULL)
pasge() ... Usage Arguments
1 R R
R
paste()
2 sep = " "
...
separate sep=" "
sep 1
> paste("A", "B", "C", "D") # sep 1
[1] "A B C D"
> paste("A", "B", "C", "D", sep="-+-") # sep -+-
[1] "A-+-B-+-C-+-D"
> paste("A", "B", "C", "D", sep="") # sep
[1] "ABCD"
4 1 R
1 "A"
"B" 1 paste() 4
paste()
I 7
3
> letters <- c("A", "B", "C", "D")
> paste(letters, 1:length(letters), sep="")
[1] "A1" "B2" "C3" "D4"
1 letters 4 2 1:length(letters)
1 4 paste()
::::::::
ID
> paste("ID", 1:15, sep="")
[1] "ID1" "ID2" "ID3" "ID4" "ID5" "ID6" "ID7" "ID8" "ID9" "ID10" "ID11"
[12] "ID12" "ID13" "ID14" "ID15"
> paste("ID", 1:dim(personal)[1], sep="") #
[1] "ID1" "ID2" "ID3" "ID4" "ID5" "ID6"
2017 1 12
> paste(2017, "/", 1:12, sep="")
[1] "2017/1" "2017/2" "2017/3" "2017/4" "2017/5" "2017/6" "2017/7" "2017/8"
[9] "2017/9" "2017/10" "2017/11" "2017/12"
1 2 1 3 12
1 2
3
> paste(rep(2017, 12), rep("/", 12), 1:12, sep="") # !
[1] "2017/1" "2017/2" "2017/3" "2017/4" "2017/5" "2017/6" "2017/7" "2017/8"
[9] "2017/9" "2017/10" "2017/11" "2017/12"
7
paste()
> results
[1] "2014 1 " "2014 2 " "2014 3 " "2014 4 " "2014 5 " "2014 6 "
[7] "2014 7 " "2014 8 " "2014 9 " "2014 10 " "2014 11 " "2014 12 "
[13] "2015 1 " "2015 2 " "2015 3 " "2015 4 " "2015 5 " "2015 6 "
[19] "2015 7 " "2015 8 " "2015 9 " "2015 10 " "2015 11 " "2015 12 "
[25] "2016 1 " "2016 2 " "2016 3 " "2016 4 " "2016 5 " "2016 6 "
[31] "2016 7 " "2016 8 " "2016 9 " "2016 10 " "2016 11 " "2016 12 "
I 8
7.1 3
7.1
> paste(c(rep(2014, 12), rep(2015, 12), rep(2016, 12)),
+ " ", 1:12, " ", sep="")
[1] "2014 1 " "2014 2 " "2014 3 " "2014 4 " "2014 5 " "2014 6 "
[7] "2014 7 " "2014 8 " "2014 9 " "2014 10 " "2014 11 " "2014 12 "
[13] "2015 1 " "2015 2 " "2015 3 " "2015 4 " "2015 5 " "2015 6 "
[19] "2015 7 " "2015 8 " "2015 9 " "2015 10 " "2015 11 " "2015 12 "
[25] "2016 1 " "2016 2 " "2016 3 " "2016 4 " "2016 5 " "2016 6 "
[31] "2016 7 " "2016 8 " "2016 9 " "2016 10 " "2016 11 " "2016 12 "
I 9

第3回 データフレームの基本操作 その1(解答付き)

  • 1.
    3 2020 1 30 11 2 data.frame 1 2.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 3 3 3.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 3.2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 3.3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 4 5 4.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6 5 6 5.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 6 : 7 7 8 7.1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 1 • 2 data.frame row column 1 R mode Numeric Character 1
  • 2.
    2.1 3 173.5 6619 166.4 58 20 168 NA 18 170.3 81 20 1: 山田太郎 西南花子 市東治子 三宅信二 リスト要素 [[1]] Character 型 ベクトル 173.5 166.4 168 170.3 リスト要素 [[2]] Numeric 型 ベクトル 男 女 女 男 リスト要素 [[5]] Character 型 ベクトル 福岡県 鹿児島県 千葉県 岡山県 リスト要素 [[6]] Character 型 ベクトル 66 58 NA 20 リスト要素 [[3]] Numeric 型 ベクトル 19 20 18 20 リスト要素 [[4]] Numeric 型 ベクトル 1: R data.frame 1 data.frame 1 1 2 NA Not Available missing values R NA logical NA 2.1 • • • row column 1 2 1 R I 2
  • 3.
    3 • • NA missingvalues 3 data.frame() > name <- c(" ", " ", " ", " ") # > height <- c(173.5, 166.4, 168, 170.3) # > weight <- c(66, 58, NA, 81) # NA > age <- c(19, 20, 18, 20) # > personal <- data.frame(name, height, weight, age) # > personal name height weight age 1 173.5 66 19 2 166.4 58 20 3 168.0 NA 18 4 170.3 81 20 3.1 cbind() column bind cbind() personal personal personal cbind() > =c(" ", " ", " ", " ") # > personal <- cbind(personal, ) # > personal name height weight age 1 173.5 66 19 2 166.4 58 20 3 168.0 NA 18 4 170.3 81 20 I 3
  • 4.
    3.2 3 > personal<- cbind(personal, =c(" ", " ", " ", " ")) > personal name height weight age 1 173.5 66 19 2 166.4 58 20 3 168.0 NA 18 4 170.3 81 20 3.2 rbind() row bind 2 > extra <- data.frame( =c(" ", " "), + =c(169, 159), + =c(61, 63), + =c(23, 20), + =c(" ", " "), + =c(" ", " ")) > extra 1 169 61 23 2 159 63 20 rbind() > rbind(personal, extra) # personal colnames() > colnames(personal) # column names [1] "name" "height" "weight" "age" " " " " > colnames(personal) <- c(" ", " ", " ", " ", " ", " ") > personal 1 173.5 66 19 2 166.4 58 20 3 168.0 NA 18 4 170.3 81 20 extra personal I 4
  • 5.
    3.3 3 > colnames(personal)<- colnames(extra) # extra personal > personal 1 173.5 66 19 2 166.4 58 20 3 168.0 NA 18 4 170.3 81 20 rbind() rbind() personal > personal <- rbind(personal, extra) > personal 1 173.5 66 19 2 166.4 58 20 3 168.0 NA 18 4 170.3 81 20 5 169.0 61 23 6 159.0 63 20 3.3 • • • • • 4 attributes 3 attributes() > attributes(personal) $names [1] " " " " " " " " " " " " 3 I 5
  • 6.
    4.1 3 $row.names [1] 12 3 4 5 6 $class [1] "data.frame" personal names row.names class 3 names row.names class colnames() names row.names rownames() > rownames(personal) # [1] "1" "2" "3" "4" "5" "6" > dim(personal) # dimension [1] 6 6 > dim(personal)[1] # [1] 6 > dim(personal)[2] # [1] 6 4.1 • • attributes() • rownames() • 5 personal 1 ID1 ID2 · · · ID > personal ID1 173.5 66 19 ID2 166.4 58 20 ID3 168.0 NA 18 ID4 170.3 81 20 ID5 169.0 61 23 ID6 159.0 63 20 I 6
  • 7.
    5.1 3 5.1 > rownames(personal)<- c("ID1", "ID2", "ID3", "ID4", "ID5", "ID6") > # paste() > rownames(personal) <- paste("ID", 1:dim(personal)[1], sep="") 6 : ID paste() paste() > help(paste) Description concatenate Usage Usage: paste (..., sep = " ", collapse = NULL) paste0(..., collapse = NULL) pasge() ... Usage Arguments 1 R R R paste() 2 sep = " " ... separate sep=" " sep 1 > paste("A", "B", "C", "D") # sep 1 [1] "A B C D" > paste("A", "B", "C", "D", sep="-+-") # sep -+- [1] "A-+-B-+-C-+-D" > paste("A", "B", "C", "D", sep="") # sep [1] "ABCD" 4 1 R 1 "A" "B" 1 paste() 4 paste() I 7
  • 8.
    3 > letters <-c("A", "B", "C", "D") > paste(letters, 1:length(letters), sep="") [1] "A1" "B2" "C3" "D4" 1 letters 4 2 1:length(letters) 1 4 paste() :::::::: ID > paste("ID", 1:15, sep="") [1] "ID1" "ID2" "ID3" "ID4" "ID5" "ID6" "ID7" "ID8" "ID9" "ID10" "ID11" [12] "ID12" "ID13" "ID14" "ID15" > paste("ID", 1:dim(personal)[1], sep="") # [1] "ID1" "ID2" "ID3" "ID4" "ID5" "ID6" 2017 1 12 > paste(2017, "/", 1:12, sep="") [1] "2017/1" "2017/2" "2017/3" "2017/4" "2017/5" "2017/6" "2017/7" "2017/8" [9] "2017/9" "2017/10" "2017/11" "2017/12" 1 2 1 3 12 1 2 3 > paste(rep(2017, 12), rep("/", 12), 1:12, sep="") # ! [1] "2017/1" "2017/2" "2017/3" "2017/4" "2017/5" "2017/6" "2017/7" "2017/8" [9] "2017/9" "2017/10" "2017/11" "2017/12" 7 paste() > results [1] "2014 1 " "2014 2 " "2014 3 " "2014 4 " "2014 5 " "2014 6 " [7] "2014 7 " "2014 8 " "2014 9 " "2014 10 " "2014 11 " "2014 12 " [13] "2015 1 " "2015 2 " "2015 3 " "2015 4 " "2015 5 " "2015 6 " [19] "2015 7 " "2015 8 " "2015 9 " "2015 10 " "2015 11 " "2015 12 " [25] "2016 1 " "2016 2 " "2016 3 " "2016 4 " "2016 5 " "2016 6 " [31] "2016 7 " "2016 8 " "2016 9 " "2016 10 " "2016 11 " "2016 12 " I 8
  • 9.
    7.1 3 7.1 > paste(c(rep(2014,12), rep(2015, 12), rep(2016, 12)), + " ", 1:12, " ", sep="") [1] "2014 1 " "2014 2 " "2014 3 " "2014 4 " "2014 5 " "2014 6 " [7] "2014 7 " "2014 8 " "2014 9 " "2014 10 " "2014 11 " "2014 12 " [13] "2015 1 " "2015 2 " "2015 3 " "2015 4 " "2015 5 " "2015 6 " [19] "2015 7 " "2015 8 " "2015 9 " "2015 10 " "2015 11 " "2015 12 " [25] "2016 1 " "2016 2 " "2016 3 " "2016 4 " "2016 5 " "2016 6 " [31] "2016 7 " "2016 8 " "2016 9 " "2016 10 " "2016 11 " "2016 12 " I 9