# 9.mutae() + ifelse() 연속형 데이터로 범주별 변수 만들기
library(dplyr)
## Warning: 패키지 'dplyr'는 R 버전 4.2.3에서 작성되었습니다
##
## 다음의 패키지를 부착합니다: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(ggplot2)
## Warning: 패키지 'ggplot2'는 R 버전 4.2.3에서 작성되었습니다
quantile(diamonds$price)
## 0% 25% 50% 75% 100%
## 326.00 950.00 2401.00 5324.25 18823.00
diamonds1<-diamonds %>% mutate(price_class=ifelse(price>=5324.25,"best",ifelse(price>=2401,
"good",ifelse(price>=950,"nomal","bad"))))
table(diamonds1$price_class)
##
## bad best good nomal
## 13483 13485 13496 13476
# 10. arrange() 데이터 정렬하기
diamonds %>% group_by(cut) %>% summarize(AvgPrice=mean(price)) %>% arrange(desc(AvgPrice))
## # A tibble: 5 × 2
## cut AvgPrice
## <ord> <dbl>
## 1 Premium 4584.
## 2 Fair 4359.
## 3 Very Good 3982.
## 4 Good 3929.
## 5 Ideal 3458.
# 11. 데이터 결합하기
ott1<-data.frame(id=c(1,2,3),car=c("bmw","bmw","bmw"),fe=c(20,22,24))
ott2<-data.frame(id=c(1,4,5),fe1=c(30,34,35))
ott1
## id car fe
## 1 1 bmw 20
## 2 2 bmw 22
## 3 3 bmw 24
ott2
## id fe1
## 1 1 30
## 2 4 34
## 3 5 35
left_join(ott1,ott2,by="id")
## id car fe fe1
## 1 1 bmw 20 30
## 2 2 bmw 22 NA
## 3 3 bmw 24 NA
#NA = 결측값
inner_join(ott1,ott2,by="id")
## id car fe fe1
## 1 1 bmw 20 30
full_join(ott1,ott2,by="id")
## id car fe fe1
## 1 1 bmw 20 30
## 2 2 bmw 22 NA
## 3 3 bmw 24 NA
## 4 4 <NA> NA 34
## 5 5 <NA> NA 35
# 행결합
ott5<-data.frame(car=c("bmw","bmw","bmw"),fe=c(20,22,24))
ott6<-data.frame(car=c("audi","audi","audi"),fe1=c(20,22,24))
bind_rows(ott5,ott6)
## car fe fe1
## 1 bmw 20 NA
## 2 bmw 22 NA
## 3 bmw 24 NA
## 4 audi NA 20
## 5 audi NA 22
## 6 audi NA 24
#factor(팩터) 모형을 만들때 중요, TRUE(순서)
q1<-c("nike","polo","adidas","wilson","yonex")
q1_factor<-as.factor(q1)
q1_factor
## [1] nike polo adidas wilson yonex
## Levels: adidas nike polo wilson yonex
as.numeric(q1_factor)
## [1] 2 3 1 4 5
factor(x=c("high school","colleage","masters"),levels=c("high school","colleage","masters"),ordered=TRUE)
## [1] high school colleage masters
## Levels: high school < colleage < masters
#문자변수에서 일부를 추출, substr(데이터셋$변수,분리 시작순서, 분리끝 순서)
economics<-ggplot2::economics
head(economics)
## # A tibble: 6 × 6
## date pce pop psavert uempmed unemploy
## <date> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 1967-07-01 507. 198712 12.6 4.5 2944
## 2 1967-08-01 510. 198911 12.6 4.7 2945
## 3 1967-09-01 516. 199113 11.9 4.6 2958
## 4 1967-10-01 512. 199311 12.9 4.9 3143
## 5 1967-11-01 517. 199498 12.8 4.7 3066
## 6 1967-12-01 525. 199657 11.8 4.8 3018
library(dplyr)
economics<-economics %>% mutate(year=substr(economics$date,1,4))
economics %>% group_by(year) %>% summarize(m=mean(psavert)) %>% arrange(desc(m)) %>% head(5)
## # A tibble: 5 × 2
## year m
## <chr> <dbl>
## 1 1971 13.5
## 2 1973 13.4
## 3 1975 13.4
## 4 1974 13.3
## 5 1970 12.8
# 시계열 데이터 가공
as.Date("2021-05-01")
## [1] "2021-05-01"
library(lubridate)
## Warning: 패키지 'lubridate'는 R 버전 4.2.3에서 작성되었습니다
##
## 다음의 패키지를 부착합니다: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
library(dplyr)
data(lakers)
lakers<-lakers %>% as_tibble
lakers %>% select(date,time)
## # A tibble: 34,624 × 2
## date time
## <int> <chr>
## 1 20081028 12:00
## 2 20081028 11:39
## 3 20081028 11:37
## 4 20081028 11:25
## 5 20081028 11:23
## 6 20081028 11:22
## 7 20081028 11:22
## 8 20081028 11:22
## 9 20081028 11:00
## 10 20081028 10:53
## # ℹ 34,614 more rows
lakers<-lakers %>% mutate(date=paste(date,time) %>% ymd_hm) %>% rename(time_index=date) %>% select(-time)
head(lakers)
## # A tibble: 6 × 12
## time_index opponent game_type period etype team player result points
## <dttm> <chr> <chr> <int> <chr> <chr> <chr> <chr> <int>
## 1 2008-10-28 12:00:00 POR home 1 jump… OFF "" "" 0
## 2 2008-10-28 11:39:00 POR home 1 shot LAL "Pau … "miss… 0
## 3 2008-10-28 11:37:00 POR home 1 rebo… LAL "Vlad… "" 0
## 4 2008-10-28 11:25:00 POR home 1 shot LAL "Dere… "miss… 0
## 5 2008-10-28 11:23:00 POR home 1 rebo… LAL "Pau … "" 0
## 6 2008-10-28 11:22:00 POR home 1 shot LAL "Pau … "made" 2
## # ℹ 3 more variables: type <chr>, x <int>, y <int>
glimpse(lakers)
## Rows: 34,624
## Columns: 12
## $ time_index <dttm> 2008-10-28 12:00:00, 2008-10-28 11:39:00, 2008-10-28 11:37…
## $ opponent <chr> "POR", "POR", "POR", "POR", "POR", "POR", "POR", "POR", "PO…
## $ game_type <chr> "home", "home", "home", "home", "home", "home", "home", "ho…
## $ period <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,…
## $ etype <chr> "jump ball", "shot", "rebound", "shot", "rebound", "shot", …
## $ team <chr> "OFF", "LAL", "LAL", "LAL", "LAL", "LAL", "POR", "LAL", "LA…
## $ player <chr> "", "Pau Gasol", "Vladimir Radmanovic", "Derek Fisher", "Pa…
## $ result <chr> "", "missed", "", "missed", "", "made", "", "made", "", "ma…
## $ points <int> 0, 0, 0, 0, 0, 2, 0, 1, 0, 2, 2, 0, 0, 2, 2, 0, 0, 2, 0, 0,…
## $ type <chr> "", "hook", "off", "layup", "off", "hook", "shooting", "", …
## $ x <int> NA, 23, NA, 25, NA, 25, NA, NA, NA, 36, 30, 34, NA, 15, 46,…
## $ y <int> NA, 13, NA, 6, NA, 10, NA, NA, NA, 21, 21, 10, NA, 17, 9, 1…