Season 1 아카이브/프로그래밍

Data Preparation (1) - Load (Dataset, Generic Variables)

크립토갈루아 2014. 11. 28. 16:21

이번 게시물부터는 R을 이용하여 모델을 만들기 위해 데이터를 다듬는 과정에 대해 이야기 해보려고 합니다. 

데이터세트를 R로 띄우고, 데이터를 관찰하고 변환하는 것에 대해 다룬 후, 예측 모델을 만드는 과정에 대해 다뤄봅니다.

이 과정에는 아래의 패키지가 필요합니다. 


> install.packages("rattle")

> library(rattle) # The weather dataset and normVarNames().


> install.packages("randomForest")

library(randomForest) # Impute missing values using na.roughfix().


> install.packages("tidyr")

> library(tidyr) # Tidy the dataset.


install.packages("ggplot2")

> library(ggplot2) # Visualise data.


install.packages("dplyr")

> library(dplyr) # Data preparation and pipes %>%.


install.packages("lubridate")

> library(lubridate) # Handle dates.


install.packages("FSelector")

> library(FSelector) # Feature selection.


1. Load-Dataset


dspatth <- "http://rattle.togaware.com/weather.csv" // 인터넷으로 연결

dspath <- system.file("csv", "weather.csv", package="rattle") // 해당 패키지에서 불러오기

> weather <- read.csv(dspath) // 변수 weather에 해당 데이터를 읽어 들인다

> library(rattle) // 또는 rattle 패키지에 weather 라는 데이터세트가 존재하고, 변수 이름 역시 weather 이므로 라이브러리 실행만으로도 충분하다. 

> dim(weather)

[1] 366  24

> names(weather)

 [1] "Date"          "Location"      "MinTemp"       "MaxTemp"      

 [5] "Rainfall"      "Evaporation"   "Sunshine"      "WindGustDir"  

 [9] "WindGustSpeed" "WindDir9am"    "WindDir3pm"    "WindSpeed9am" 

[13] "WindSpeed3pm"  "Humidity9am"   "Humidity3pm"   "Pressure9am"  

[17] "Pressure3pm"   "Cloud9am"      "Cloud3pm"      "Temp9am"      

[21] "Temp3pm"       "RainToday"     "RISK_MM"       "RainTomorrow" 

> str(weather)

'data.frame': 366 obs. of  24 variables:

 $ Date         : Date, format: "2007-11-01" "2007-11-02" ...

 $ Location     : Factor w/ 49 levels "Adelaide","Albany",..: 10 10 10 10 10 10 10 10 10 10 ...

2. Load-Generic Variables


> dsname <- "weather"

> ds <- get(dsname)

> dim(ds)

[1] 366  24

> names(ds)

 [1] "Date"          "Location"      "MinTemp"       "MaxTemp"      

 [5] "Rainfall"      "Evaporation"   "Sunshine"      "WindGustDir"  

 [9] "WindGustSpeed" "WindDir9am"    "WindDir3pm"    "WindSpeed9am" 

[13] "WindSpeed3pm"  "Humidity9am"   "Humidity3pm"   "Pressure9am"  

[17] "Pressure3pm"   "Cloud9am"      "Cloud3pm"      "Temp9am"      

[21] "Temp3pm"       "RainToday"     "RISK_MM"       "RainTomorrow" 



728x90