문자로 된 날짜 데이터에서 요일 데이터 추출하기
다음 데이터에서
> temp
[1] "2015-06-09" "2015-06-10" "2015-06-11" "2015-06-12" "2015-06-13" "2015-06-14" "2015-06-15" "2015-06-16"
[9] "2015-06-17" "2015-06-18" "2015-06-19" "2015-06-20" "2015-06-21" "2015-06-22" "2015-06-23" "2015-06-24"
[17] "2015-06-25" "2015-06-26" "2015-06-27" "2015-06-28" "2015-06-29" "2015-06-30" "2015-07-01" "2015-07-02"
[25] "2015-07-03" "2015-07-04" "2015-07-05" "2015-07-06" "2015-07-07" "2015-07-08" "2015-07-09" "2015-07-10"
[33] "2015-07-11" "2015-07-12" "2015-07-13" "2015-07-14" "2015-07-15" "2015-07-16" "2015-07-17" "2015-07-18"
[41] "2015-07-19" "2015-07-20" "2015-07-21" "2015-07-22" "2015-07-23" "2015-07-24" "2015-07-25" "2015-07-26"
[49] "2015-07-27" "2015-07-28" "2015-07-29" "2015-07-30" "2015-07-31" "2015-08-01" "2015-08-02" "2015-08-03"
[57] "2015-08-04" "2015-08-05" "2015-08-06" "2015-08-07" "2015-08-08" "2015-08-09" "2015-08-10" "2015-08-11"
[65] "2015-08-12" "2015-08-13" "2015-08-14" "2015-08-15" "2015-08-16" "2015-08-17" "2015-08-18" "2015-08-19"
[73] "2015-08-20" "2015-08-21" "2015-08-22" "2015-08-23" "2015-08-24" "2015-08-25" "2015-08-26" "2015-08-27"
[81] "2015-08-28" "2015-08-29" "2015-08-30" "2015-08-31" "2015-09-01" "2015-09-02" "2015-09-03" "2015-09-04"
[89] "2015-09-05" "2015-09-06" "2015-09-07" "2015-09-08" "2015-09-09" "2015-09-10" "2015-09-11" "2015-09-12"
[97] "2015-09-13" "2015-09-14" "2015-09-15" "2015-09-16"
요일을 뽑아 봅시다.
extract_weekday <- function(str_date) {
posixlt_date <- as.POSIXlt(str_date)
posixlt_date$wday
}
transform_weekcode <- function(wday) {
wday.before <- factor(wday,levels=c(0,1,2,3,4,5,6))
levels(wday.before) <- c("Sun","Mon","Tue","Wed","Thr","Fri","Sat")
wday.after <- as.character(wday.before)
wday.after
}
이상의 두 개 함수를 실행한 다음
> temp2 <- extract_weekday(temp)
> temp2
[1] 2 3 4 5 6 0 1 2 3 4 5 6 0 1 2 3 4 5 6 0 1 2 3 4 5 6 0 1 2 3 4 5 6 0 1 2 3 4 5 6 0 1 2 3 4 5 6 0 1 2 3 4 5
[54] 6 0 1 2 3 4 5 6 0 1 2 3 4 5 6 0 1 2 3 4 5 6 0 1 2 3 4 5 6 0 1 2 3 4 5 6 0 1 2 3 4 5 6 0 1 2 3
이때, 0이 일요일, 1이 월요일, ...
보기 좋게 문자로 표현하면
> temp3 <- transform_weekcode(temp2)
> temp3
[1] "Tue" "Wed" "Thr" "Fri" "Sat" "Sun" "Mon" "Tue" "Wed" "Thr" "Fri" "Sat" "Sun" "Mon" "Tue" "Wed" "Thr"
[18] "Fri" "Sat" "Sun" "Mon" "Tue" "Wed" "Thr" "Fri" "Sat" "Sun" "Mon" "Tue" "Wed" "Thr" "Fri" "Sat" "Sun"
[35] "Mon" "Tue" "Wed" "Thr" "Fri" "Sat" "Sun" "Mon" "Tue" "Wed" "Thr" "Fri" "Sat" "Sun" "Mon" "Tue" "Wed"
[52] "Thr" "Fri" "Sat" "Sun" "Mon" "Tue" "Wed" "Thr" "Fri" "Sat" "Sun" "Mon" "Tue" "Wed" "Thr" "Fri" "Sat"
[69] "Sun" "Mon" "Tue" "Wed" "Thr" "Fri" "Sat" "Sun" "Mon" "Tue" "Wed" "Thr" "Fri" "Sat" "Sun" "Mon" "Tue"
[86] "Wed" "Thr" "Fri" "Sat" "Sun" "Mon" "Tue" "Wed" "Thr" "Fri" "Sat" "Sun" "Mon" "Tue" "Wed"
데이터 중, 주중과 주말을 구분하는 방법은 간단합니다.
주말은 토요일(=6)과 일요일(=0)입니다.
> temp2 %in% c(0,6)
[1] FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE FALSE
[18] FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE TRUE TRUE
[35] FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE
[52] FALSE FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE TRUE
[69] TRUE FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE
[86] FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE
이제 TRUE는 주말, FALSE는 주중입니다.
감사합니다.
> temp
[1] "2015-06-09" "2015-06-10" "2015-06-11" "2015-06-12" "2015-06-13" "2015-06-14" "2015-06-15" "2015-06-16"
[9] "2015-06-17" "2015-06-18" "2015-06-19" "2015-06-20" "2015-06-21" "2015-06-22" "2015-06-23" "2015-06-24"
[17] "2015-06-25" "2015-06-26" "2015-06-27" "2015-06-28" "2015-06-29" "2015-06-30" "2015-07-01" "2015-07-02"
[25] "2015-07-03" "2015-07-04" "2015-07-05" "2015-07-06" "2015-07-07" "2015-07-08" "2015-07-09" "2015-07-10"
[33] "2015-07-11" "2015-07-12" "2015-07-13" "2015-07-14" "2015-07-15" "2015-07-16" "2015-07-17" "2015-07-18"
[41] "2015-07-19" "2015-07-20" "2015-07-21" "2015-07-22" "2015-07-23" "2015-07-24" "2015-07-25" "2015-07-26"
[49] "2015-07-27" "2015-07-28" "2015-07-29" "2015-07-30" "2015-07-31" "2015-08-01" "2015-08-02" "2015-08-03"
[57] "2015-08-04" "2015-08-05" "2015-08-06" "2015-08-07" "2015-08-08" "2015-08-09" "2015-08-10" "2015-08-11"
[65] "2015-08-12" "2015-08-13" "2015-08-14" "2015-08-15" "2015-08-16" "2015-08-17" "2015-08-18" "2015-08-19"
[73] "2015-08-20" "2015-08-21" "2015-08-22" "2015-08-23" "2015-08-24" "2015-08-25" "2015-08-26" "2015-08-27"
[81] "2015-08-28" "2015-08-29" "2015-08-30" "2015-08-31" "2015-09-01" "2015-09-02" "2015-09-03" "2015-09-04"
[89] "2015-09-05" "2015-09-06" "2015-09-07" "2015-09-08" "2015-09-09" "2015-09-10" "2015-09-11" "2015-09-12"
[97] "2015-09-13" "2015-09-14" "2015-09-15" "2015-09-16"
요일을 뽑아 봅시다.
extract_weekday <- function(str_date) {
posixlt_date <- as.POSIXlt(str_date)
posixlt_date$wday
}
transform_weekcode <- function(wday) {
wday.before <- factor(wday,levels=c(0,1,2,3,4,5,6))
levels(wday.before) <- c("Sun","Mon","Tue","Wed","Thr","Fri","Sat")
wday.after <- as.character(wday.before)
wday.after
}
이상의 두 개 함수를 실행한 다음
> temp2 <- extract_weekday(temp)
> temp2
[1] 2 3 4 5 6 0 1 2 3 4 5 6 0 1 2 3 4 5 6 0 1 2 3 4 5 6 0 1 2 3 4 5 6 0 1 2 3 4 5 6 0 1 2 3 4 5 6 0 1 2 3 4 5
[54] 6 0 1 2 3 4 5 6 0 1 2 3 4 5 6 0 1 2 3 4 5 6 0 1 2 3 4 5 6 0 1 2 3 4 5 6 0 1 2 3 4 5 6 0 1 2 3
이때, 0이 일요일, 1이 월요일, ...
보기 좋게 문자로 표현하면
> temp3 <- transform_weekcode(temp2)
> temp3
[1] "Tue" "Wed" "Thr" "Fri" "Sat" "Sun" "Mon" "Tue" "Wed" "Thr" "Fri" "Sat" "Sun" "Mon" "Tue" "Wed" "Thr"
[18] "Fri" "Sat" "Sun" "Mon" "Tue" "Wed" "Thr" "Fri" "Sat" "Sun" "Mon" "Tue" "Wed" "Thr" "Fri" "Sat" "Sun"
[35] "Mon" "Tue" "Wed" "Thr" "Fri" "Sat" "Sun" "Mon" "Tue" "Wed" "Thr" "Fri" "Sat" "Sun" "Mon" "Tue" "Wed"
[52] "Thr" "Fri" "Sat" "Sun" "Mon" "Tue" "Wed" "Thr" "Fri" "Sat" "Sun" "Mon" "Tue" "Wed" "Thr" "Fri" "Sat"
[69] "Sun" "Mon" "Tue" "Wed" "Thr" "Fri" "Sat" "Sun" "Mon" "Tue" "Wed" "Thr" "Fri" "Sat" "Sun" "Mon" "Tue"
[86] "Wed" "Thr" "Fri" "Sat" "Sun" "Mon" "Tue" "Wed" "Thr" "Fri" "Sat" "Sun" "Mon" "Tue" "Wed"
데이터 중, 주중과 주말을 구분하는 방법은 간단합니다.
주말은 토요일(=6)과 일요일(=0)입니다.
> temp2 %in% c(0,6)
[1] FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE FALSE
[18] FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE TRUE TRUE
[35] FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE
[52] FALSE FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE TRUE
[69] TRUE FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE
[86] FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE FALSE FALSE TRUE TRUE FALSE FALSE FALSE
이제 TRUE는 주말, FALSE는 주중입니다.
감사합니다.
댓글
댓글 쓰기