"R 데이터프레임 Date형 지정"의 두 판 사이의 차이

잔글 (봇: 자동으로 텍스트 교체 (-</source> +</syntaxhighlight>, -<source +<syntaxhighlight ))
6번째 줄: 6번째 줄:


==문제상황==
==문제상황==
<source lang='r' notebook>
<syntaxhighlight lang='r' notebook>
df <- read.table( header=T, stringsAsFactors=FALSE, text="
df <- read.table( header=T, stringsAsFactors=FALSE, text="
day        fruit    ea
day        fruit    ea
16번째 줄: 16번째 줄:
")
")
df
df
</source>
</syntaxhighlight>
<source lang='r' notebook>
<syntaxhighlight lang='r' notebook>
class(df$day)
class(df$day)
</source>
</syntaxhighlight>


==방법1: as.Date() ★==
==방법1: as.Date() ★==
<source lang='r' notebook>
<syntaxhighlight lang='r' notebook>
df$day1 <- as.Date(df$day)
df$day1 <- as.Date(df$day)
df
df
</source>
</syntaxhighlight>
<source lang='r' notebook>
<syntaxhighlight lang='r' notebook>
class(df$day1)
class(df$day1)
</source>
</syntaxhighlight>


==방법2: ymd()==
==방법2: ymd()==
<source lang='r' notebook>
<syntaxhighlight lang='r' notebook>
library("lubridate")
library("lubridate")
df$day2 <- ymd(df$day)
df$day2 <- ymd(df$day)
df
df
</source>
</syntaxhighlight>
<source lang='r' notebook>
<syntaxhighlight lang='r' notebook>
class(df$day2)
class(df$day2)
</source>
</syntaxhighlight>


==같이 보기==
==같이 보기==

2021년 10월 20일 (수) 13:53 판

1 개요

R 데이터프레임 날짜형 지정
R 데이터프레임 날짜를 Date형으로 변환
R 데이터프레임 날짜를 Date형으로 지정
  • Date형은 날짜 연산이 수월하다.

2 문제상황

df <- read.table( header=T, stringsAsFactors=FALSE, text="
day         fruit    ea
2019-06-01  apple    1
2019-06-11  apple    1
2019-06-21  banana   2
2019-07-01  apple    3
2019-07-11  banana   4
")
df
class(df$day)

3 방법1: as.Date() ★

df$day1 <- as.Date(df$day)
df
class(df$day1)

4 방법2: ymd()

library("lubridate")
df$day2 <- ymd(df$day)
df
class(df$day2)

5 같이 보기

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}