본문 바로가기

R

tidyr:: (2) Split or combine: separate, separate_rows, unite

[참고 페이지]

https://m.blog.naver.com/bosongmoon/221587219214

https://gomguard.tistory.com/22 (추가 argument 설명 有)

 

tidy data 재설명

 

 


 

 

지금 설명할 것은,
'Use these functions to split or combine cells into individual, isolated values'에 관련된 두 함수다.

 

 

 

Spilt or combine Cells

 

(1) separate(data, col, into, sep, remove = T, convert = F, extra = "warn", fill = "warn", ...)

tidyr의 table3 data 사용

table3 %>% separate(rate, into= c("'cases', 'pop'))

또는,

separate(table3, rate, into= c("'cases', 'pop'))

= table3$rate를 "/"를 기준으로 'cases', 'pop'으로 쪼갠다.

[NOTE] 구분자를 넣고 싶으면, sep="/"을 사용하자!

 

 


 

 

(2) separate_rows(data, sep, convert = FALSE) 

동일하게 table3 사용

table3 %>% separate_rows(rate)

또는,

separate_rows(table3, rate)

= table3$rate를 separate로 쪼개어 여러 row를 생성한다. (rate만 다른 여러 행 생성)

 

 


 

 

(3) unite(data, col, sep="_", remove=T)

table5

table5 %>% unite(century, year, col="year", sep="")

또는,

unite(table5, century, year, col = "year", sep = "")

= century와 year의 값을 이어붙인다. 이때 sep을 사용하여 사이 값을 정해준다.

'R' 카테고리의 다른 글

tidyr:: (3) iris data에 적용해보자 (+pivot_longer)  (0) 2022.07.21
tidyr:: (1) reshape function: gather, spread  (0) 2022.07.21
'tidyverse' package  (0) 2022.07.21