본문 바로가기
카테고리 없음

[전처리] R로 데이터 추출하기 (기본)

by 인디코더 2020. 2. 13.

1. 데이터 읽기 

reserve_tb <- read.csv('[경로]/[파일이름]', fileEncoding = 'UTF-8', header = TRUE, stringsAsFactors = FALSE)

> fileEncoding : 읽어올 파일의 인코딩 설정
> header : 헤더 유무 설정 
> stringsAsFactors : False로 지정하여 문자열을 번주형으로 변경하지 않도록 함.

2. 라이브러리 적용

library(dplyr)                                      # 라이브러리 적용
detach("package:dplyr", unload =TRUE)   # 라이브러리 해제

3. dplyr  (sql함수를 적용할 수 있다, pipe함수를 사용할 수 있다.)
   %>% : shortcut(ctrl+shift+m)

# pipe함수를 통해 매개변수를 전달해 줄 수 있다. 

reserve_tb reserve_tb %>% 
  select(reserve_id, hotel_id, customer_id, reserve_datetime, checkin_date, checkin_time, checkout_date) %>% 
  as.data.frame()

# dplyr 함수를 쓰면 dataframe이 dplyr 로 바뀐다고 함. as.data.frame()으로 다시 R의 dataframe으로 변경해줌. 
# 체감은 안됨.

+ 추가 함수들 , 추출할때는 그렇게 좋지 않은... 

reserve_tb %>% select(starts_with('check'))
reserve_tb %>% select(ends_with('id'))
reserve_tb %>% select(contains('omer'))
reserve_tb %>% select(matches('id'))

반응형