May, 2018
– Survey 데이터를 부르고 열 이름을 확인하자.
## used (Mb) gc trigger (Mb) max used (Mb) ## Ncells 482619 25.8 940480 50.3 750400 40.1 ## Vcells 868543 6.7 1650153 12.6 1135658 8.7
surveys
데이터프레임에서 "plot_id", "species_id", "weight" 열을 골라보자names(surveys)
는 데이터프레임의 열 이름을 출력하는 함수다.match
함수의 용법은 R-graphical tool, R Basic(Review) 단원을 참고하여라.match(c("plot_id", "species_id", "weight"), names(surveys)) surveys[, match(c("plot_id", "species_id", "weight"), names(surveys)) ]
surveys[,c("plot_id", "species_id", "weight") ]
surveys[c("plot_id", "species_id", "weight")]
year
값이 1995인 데이터 행만 추출하고자 한다.surveys$year == 1995 surveys[surveys$year == 1995, ] head(surveys[surveys$year == 1995, ])
weight
값이 5미만인 데이터에 대해서 "species_id", "sex", "weight" 열을 보고자 한다.surveys[surveys$weight<5, c("species_id", "sex", "weight")]
surveys[which(surveys$weight<5), c("species_id", "sex", "weight")]
surveys
데이터에 weight_kg
열을 추가하여 surveys_ex
데이터프레임을 생성하고자 한다. weight_kg
열은 weight
열을 1000으로 나눈 값을 가진다. 단, surveys_ex
데이터는 weight_kg
값이 NA
인 것은 제외한다.cbind()
를 사용하는 것도 가능하지만, list
에서 데이터를 추가하는 방법을 사용하는 것도 가능하다.is.na()
를 사용하여 데이터프레임을 slicing 한다.surveys_ex <- surveys surveys_ex$weight_kg <- surveys_ex$weight/1000 surveys_ex <- surveys_ex[!is.na(surveys_ex$weight_kg),]
surveys
데이터에서 sex
별로 weight
값의 평균을 계산하고자 한다. 단, NA
값은 제외하고 계산하고자 한다.sex
의 유일한 값은 어떻게 되나?u = unique(surveys$sex) length(u) class(surveys$sex) levels(surveys$sex)
surveys$sex
가 factor
이므로 levels()
함수를 사용할 수 있다. 만약 그 변수가 character
면 unique
함수를 사용해야 한다.sex
별로 weight
값의 평균을 계산mean( surveys$weight[surveys$sex == u[1]], na.rm = T ) mean( surveys$weight[surveys$sex == u[2]], na.rm = T ) mean( surveys$weight[surveys$sex == u[3]], na.rm = T )
by
함수의 이용by( data = surveys$weight, INDICES = surveys$sex, FUN = mean, na.rm = TRUE)
aggregate
함수의 이용aggregate(formula = weight ~ sex, data = surveys, FUN = mean, na.rm = TRUE)
sex
와 species_id
별로 weight
값의 평균을 계산by
문을 사용할 수 있지만, 다음과 같이 aggregate
함수를 이용해 간단히 표현할 수 있다.aggregate(formula = weight ~ sex + species_id, data = surveys, FUN = mean, na.rm = TRUE)
sex
와 species_id
별로 weight
값의 평균과 분산과 최소값과 최대값의 계산surveys
데이터의 성별의 빈도, (성별, plot_id
)빈도를 계산하여라.table(surveys$sex) table(surveys$sex, surveys$plot_id)
plot_id
값의 오름차순으로 surveys
데이터를 정렬하여라.surveys[order(surveys$plot_id),]
month
값을 내림차순으로 정렬하고 그 순서를 유지시키면서 plot_id
값의 오름차순으로 surveys
데이터를 정렬하여라.tmp <- surveys tmp <- tmp[order(tmp$plot_id),] tmp <- tmp[order(tmp$month, decreasing = TRUE),] head(tmp)
Hadley Wickham 은 뉴질랜드 통계학자로 RStudio 수석 과학자, Rice University의 겸임교수로 재직중임.
dplyr 의 중요한 함수로는 select
, filter
, mutate
, group_by
, summarize
와 연산자인 파이프함수 %>%
가 있다.
dplyr
는 디플라이어 (데이터 플라이어: 데이터를 다루는 작은 집게)라고 흔히 부른다.
특히 파이프라인 연산자는 코드의 가독성을 높여주는데 큰 기여를 한다.
if (!require(dplyr)) { install.packages("dplyr") ; library(dplyr) }
surveys
데이터프레임에서 "plot_id", "species_id", "weight" 열을 골라보자.select
함수를 이용하여 고를 수 있다.select(.data = surveys, plot_id, species_id, weight)
%>%
의 사용방법을 확인하여라.head(select(surveys, plot_id, species_id, weight)) select(.data = surveys, plot_id, species_id, weight) %>% head()
year
값이 1995인 데이터 행만 추출하고자 한다.filter
는filter(.data = surveys, year == 1995) %>% head()
year
값이 1995이상이고 weight
가 20보다 큰 데이터 행만 추출하고자 한다.filter(.data = surveys, year >= 1995 & weight > 20) %>% head() filter(.data = surveys, year >= 1995 , weight > 20) %>% head()
weight
값이 5미만인 데이터에 대해서 "species_id", "sex", "weight" 열을 보고자 한다. 그리고 weight
가 NA인 것은 제외하고 싶다.surveys %>% filter( !is.na(weight) ) %>% filter(weight < 5) %>% select(species_id, sex, weight) %>% head()
surveys
데이터에 weight_kg
열을 추가하여 surveys_ex
데이터프레임을 생성하고자 한다. weight_kg
열은 weight
열을 1000으로 나눈 값을 가진다. 단, surveys_ex
데이터는 weight_kg
값이 NA
인 것은 제외한다.surveys_ex <- surveys %>% filter( !is.na(surveys$weight)) %>% mutate(weight_kg = weight / 1000)
surveys
데이터에서 sex
별로 weight
값의 평균을 계산하고자 한다. 단, NA
값은 제외하고 계산하고자 한다.surveys %>% group_by(sex) %>% summarize(mean_weight = mean(weight, na.rm = TRUE))
sex
와 species_id
별로 weight
값의 평균을 계산surveys %>% filter(!is.na(weight)) %>% group_by(sex, species_id) %>% summarize(mean_weight = mean(weight, na.rm = TRUE))
sex
와 species_id
별로 weight
값의 평균과 분산과 최소값과 최대값의 계산surveys %>% filter(!is.na(weight)) %>% group_by(sex, species_id) %>% summarize(mean_weight = mean(weight), var_weight = var(weight), min_weight = min(weight), max_weight = max(weight)) %>% print(n = 5)
surveys
데이터의 성별의 빈도, (성별, plot_id
)빈도를 계산하여라.surveys %>% group_by(sex) %>% tally() surveys %>% group_by(plot_id, sex) %>% tally()
plot_id
값의 오름차순으로 surveys
데이터를 정렬하여라.surveys %>% arrange(month, plot_id) %>% head()
month
값을 내림차순으로 정렬하고 그 순서를 유지시키면서 plot_id
값의 오름차순으로 surveys
데이터를 정렬하여라.surveys %>% arrange(desc(month), plot_id) %>% head()
## Ozone Solar.R Wind Temp Month Day ## 1 41 190 7.4 67 5 1 ## 2 36 118 8.0 72 5 2 ## 3 12 149 12.6 74 5 3
## No id variables; using all as measure variables
## variable value ## 1 Ozone 41 ## 2 Ozone 36 ## 3 Ozone 12
데이터 분석시 사용되는 함수에 따라 요구하는 데이터의 포맷이 다를 수 있다.
Wide format: almost modeling function such as lm
, glm
, gam
,…
Long format: ggplot2
reshape2
라이브러리는 데이터 포맷의 변경 및 간단한 요약통계량을 산출해주는 함수를 가지고 있다.
melt
함수는 wide format 데이터를 long format 데이터로 변경해준다.names(airquality) <- tolower(names(airquality)) melt(data = airquality) %>% head(n=3)
## No id variables; using all as measure variables
## variable value ## 1 ozone 41 ## 2 ozone 36 ## 3 ozone 12
melt
함수에 id.vars
을 지정해줌으로써 long format은 행을 구분해 줄 수 있는 식별자를 지정해줄 수 있다.names(airquality) <- tolower(names(airquality)) aql <- melt(data = airquality, id.vars= c("month","day")) head(aql, n = 3)
## month day variable value ## 1 5 1 ozone 41 ## 2 5 2 ozone 36 ## 3 5 3 ozone 12
variable
과 value
에 값의 이름을 지정해 줄 수 있다.## month day climate_variable climate_value ## 1 5 1 ozone 41 ## 2 5 2 ozone 36 ## 3 5 3 ozone 12
knitr::include_graphics("./fig/reshape2-1.JPG")
if (!require(reshape2)) { install.packages("reshape2") ; library(reshape2) } aqw <- dcast(aql, month + day ~ climate_variable, value.var ="climate_value")
dcast(aql, month ~ climate_variable, fun.aggregate = mean, na.rm = TRUE, margins = TRUE) %>% head(n=3)
## month ozone solar.r wind temp (all) ## 1 5 23.61538 181.2963 11.622581 65.54839 68.70696 ## 2 6 29.44444 190.1667 10.266667 79.10000 87.38384 ## 3 7 59.11538 216.4839 8.941935 83.90323 93.49748
- 여기서 `margins = TRUE` 를 생략하고 결과를 비교하여라. - `margins`와 관련한 다른 option 을 찾아보아라.
StarCraft 2 replay file에서 스크린 내에서 발생하는 플레이어의 입력 정보를 Salvucci & Goldberg (2000) dispersion-threshold 알고리즘을 이용하여 1초당 약 88.5개의 시분할 작업을 수집하였음
StarCraft 2 replay file: 하나의 게임이 저장되어 게임 내에서 일어난 전체정보를 담고 있는 파일. 이 파일로 기록된 게임의 모든 정보를 얻을 수 있음
LeagueIndex: Bronze, Silver, Gold, Platinum, Diamond, Master, GrandMaster, and Professional leagues coded 1-8 (Ordinal)
SelectByHotkeys: 하나의 타임 프레임에서 (1/85.5 sec) 핫키 사용 평균 횟수 ( 핫키: 단축키)
PAC : Perception-Action-Cycles. 특정 유닛을 클릭한 후 명령을 완료하는 동작 NumberOfPACs
ComplexAbilitiesUsed
와인의 화학적 성분을 실험실에서 측정한 다음, 그것들의 특징을 찾는 작업
물리화학적 평가와 감각에 의한 평가에 대한 관계가 있을것으로 예상하나 몇몇 연구에 제한되어 있음
official certification entity (CVRVV)에 제공하는 데이터
데이터의 수: 1599(레드와인), 4898(화이트 와인)
변수: 총 12개, ( 평가점수 (0 (very bad)~10점 (excellent); 11개의 물리화학적 성분)