Script1 )
# 워킹 디렉토리를 불러온다.
# <워킹 디렉토리>는 앞으로 코딩할 R소스가 담긴 폴더이다.
getwd()
# 워킹 디렉토리를 설정한다.
#setwd('C:/Sources')
# 패키지 형태인 데이터를 가져온다.
install.packages("dplyr")
install.packages("ggplot2")
# 패키지를 사용하기 위해 라이브러리에 추가한다.
library(dplyr)
library(ggplot2)
# 데이터의 구조를 나타낸다.
str(iris)
# 데이터를 별도의 창에 나타낸다.
View(iris)
# iris데이터 위에 10개값을 보겠다. <반대는 tail>
head(iris, 10)
# 데이터를 시각화해서 나타낸다.
plot(iris)
plot(iris$Petal.Width, iris$Petal.Length, col = iris$Species)
# 외부에 있는 데이터를 가져온다.
tips = read.csv('https://raw.githubusercontent.com/mwaskom/seaborn-data/master/tips.csv')
str(tips)
head(tips, 10)
# 요약 통계를 보여줌
summary(tips)
# 그림 2-19(a)~(d)
# %>%은 dplyr패키지가 제공하는 명령어로, 패키지 함수에 연결하는 연산자이다. <즉 tips에서 ggplot2패키지의 함수(명령어)를 사용>
# size변수의 히스토그램을 보여준다.
# aes변수로 나타낼 변수를 지정할 수 있고 옵션(색,기호 등)도 지정할 수 있다.
tips%>%ggplot(aes(size)) + geom_histogram() # 그림 2-19(a)
# total_bill변수와 tip변수의 산점도(두 변수 사이의 관계도)를 보여준다.
tips%>%ggplot(aes(total_bill, tip)) + geom_point() # 그림 2-19(b)
# 마찬가지로 산점도를 보여주데 요일은 색으로 나타낸다.
tips%>%ggplot(aes(total_bill, tip)) + geom_point(aes(col = day)) # 그림 2-19(c)
# 마찬가지로 산점도를 보여주는데 성별은 다른 모양으로 나타낸다. 그리고 size옵션으로 기호의 크기를 키운다.
tips%>%ggplot(aes(total_bill, tip)) + geom_point(aes(col = day, pch = sex), size = 3) # 그림 2-19(d)
Script2)
# 이 데이터가 무슨 데이터인지 알아본다. ?cars View(cars) summary(cars) plot(cars) # box형태로 데이터를 시각화한다. boxplot(cars)
북 참고) R로 배우는 데이터 과학