R 기초; 데이터프레임 확장
# 데이터프레임 확장
id <- c("A001", "A002", "A003")
name <- c("Mouse", "Keyboard", "USB")
price <- c(30000, 90000, 50000)
product <- data.frame(id, name, price, stringsAsFactors=FALSE)
product
id name price
1 A001 Mouse 30000
2 A002 Keyboard 90000
3 A003 USB 50000
# 데이터프레임 행 추가 using rbind()
product <- rbind(product, c("A004", "Monitor", 250000))
product
id name price
1 A001 Mouse 30000
2 A002 Keyboard 90000
3 A003 USB 50000
4 A004 Monitor 250000
# 데이터프레임 행추가 using new.rows
new.rows <- data.frame(id=c("A005", "A006"), name=c("Memory", "CPU"), price=c(35000, 320000))
new.rows
id name price
1 A005 Memory 35000
2 A006 CPU 320000
product <- rbind(product, new.rows)
# 열의 방향으로 데이터프레임 확장하기
product <- cbind(product, madein=c("Korea", "China", "China", "Korea"))
product$madein <- c("Korea", "China", "China", "Korea")
product
id name price madein
1 A001 Mouse 30000 Korea
2 A002 Keyboard 90000 China
3 A003 USB 50000 China
4 A004 Monitor 250000 Korea
# 데이터프레임 열 추가하기 using new.cols
new.cols <- data.frame(manufacturer = c("Logitech", "Logitech", "Samsung", "Samsung", "Samsung", "Intel"), quantity=c(20, 15, 50, 30, 40, 10))
new.cols
manufacturer quantity
1 Logitech 20
2 Logitech 15
3 Samsung 50
4 Samsung 30
5 Samsung 40
6 Intel 10
product <- cbind(proudct, new.cols)
product
cols1 <- data.frame(x=c("a", "b", "c"), y=c(1, 2, 3))
cols2 <- data.frame(x=c("alpha", "beta", "gamma"), y=c(100, 200, 300))
# 두개의 데이터프레임을 열 방향으로 합치기 (merge)
cbind(cols1, cols2)
x y x y
1 a 1 alpha 100
2 b 2 beta 200
3 c 3 gamma 300
# 데이터프레임 생성 (신생아 데이터)
df1 <- data.frame(sex="female", months=1, weight=3.5)
df2 <- data.frame(sex="male", months=3, weight=4.8)
df3 <- data.frame(sex="male", months=4, weight=5.3)
df4 <- data.frame(sex="female", months=9, weight=9.4)
df5 <- data.frame(sex="female", months=7, weight=8.3)
lst <- list(df1, df2, df3, df4, df5)
lst
[[1]]
sex months weight
1 female 1 3.5
[[2]]
sex months weight
1 male 3 4.8
[[3]]
sex months weight
1 male 4 5.3
[[4]]
sex months weight
1 female 9 9.4
[[5]]
sex months weight
1 female 7 8.3
lst[[1]]
sex months weight
1 female 1 3.5
lst[[2]]
sex months weight
1 male 3 4.8
# 리스트 원소를 이용해서 데이터프레임 합치기
rbind(lst[[1]], lst[[2]]
# do.call
do.call(rbind, lst)
sex months weight
1 female 1 3.5
2 male 3 4.8
3 male 4 5.3
4 female 9 9.4
5 female 7 8.3
lst1 <- list(sex="female", months=1, weight=3.5)
lst2 <- list(sex="male", months=3, weight=4.8)
lst3 <- list(sex="male", months=4, weight=5.3)
lst4 <- list(sex="female", months=9, weight=9.4)
lst5 <- list(sex="female", months=7, weight=8.3)
# nested lists
lst <- list(lst1, lst2, lst3, lst4, lst5)
lst
[[1]]
[[1]]$sex
[1] "female"
[[1]]$months
[1] 1
[[1]]$weight
[1] 3.5
[[2]]
[[2]]$sex
[1] "male"
[[2]]$months
[1] 3
[[2]]$weight
[1] 4.8
[[3]]
[[3]]$sex
[1] "male"
[[3]]$months
[1] 4
[[3]]$weight
[1] 5.3
[[4]]
[[4]]$sex
[1] "female"
[[4]]$months
[1] 9
[[4]]$weight
[1] 9.4
[[5]]
[[5]]$sex
[1] "female"
[[5]]$months
[1] 7
[[5]]$weight
[1] 8.3
lst[[1]]
$sex
[1] "female"
$months
[1] 1
$weight
[1] 3.5
as.data.frame(lst[[1]])
sex months weight
1 female 1 3.5
lapply(lst, as.data.frame)
[[1]]
sex months weight
1 female 1 3.5
[[2]]
sex months weight
1 male 3 4.8
[[3]]
sex months weight
1 male 4 5.3
[[4]]
sex months weight
1 female 9 9.4
[[5]]
sex months weight
1 female 7 8.3
do.call(rbind, lapply(lst, as.data.frame))
sex months weight
1 female 1 3.5
2 male 3 4.8
3 male 4 5.3
4 female 9 9.4
5 female 7 8.3
'공부 > R Programming' 카테고리의 다른 글
R기초; 데이터프레임 인덱싱 - 2 (0) | 2021.01.10 |
---|---|
R기초; 데이터프레임 인덱싱 - 1 (0) | 2021.01.09 |
R기초; 데이터프레임 생성 (0) | 2021.01.06 |
R 기초; 리스트 인덱싱-2 (0) | 2021.01.06 |
R 기초; 리스트 인덱싱-1 (0) | 2021.01.05 |
댓글