본문 바로가기
공부/R Programming

R 기초; 형태 변환2 - tidyr

by 혼밥맨 2021. 1. 23.
반응형

R 기초; 형태 변환2 - tidyr

 

gather()

spread()

 

install.packages("tidyr")

library(tidyr)

 

# 뉴욕시 5개월 기간 동안 대기환경 정보

head(airquality)

 

?gather

aq.long <- gather(airquality, key=Factor, value=Measurement, Ozone: Temp)

 

head(aq.long)

 

aq.long <- gather(airquality, key=Factor, value=Measurement, 1:4)

 

head(aq.long)

 

aq.long <- gather(airquality, key=Factor, value=Measurement, Ozone, Solar.R, Wind, Temp)

 

head(aq.long)

 

airquality %>% gather(Factor, Measurement, Ozone:Temp)

 

aq.wide <- spread(data=aq.long, key=Factor, value=Measurement)

head(aq.wide)

 

aq.long %>% spread(key=Factor, value=Measurement)

 

separate()

unite()

 

head(iris)

iris.long <- gather(iris, Element, Measurement, -Species)

 

head(iris.long)
iris.sep <- separate(data=iris.long, col=Element, into=c("Part", "Measures"))

 

head(iris.sep)

 

iris.unite <- unite(iris.sep)

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

반응형

댓글