데이터 R지?
12차시_대면수업
jsBae
2022. 5. 16. 22:12
9.3.3 결측치 처리
데이터가 비어있는 값을 결측치(Missing Value) 라고 한다
종류 | 의미 | 설명 |
is.na() | 결측치 확인 및 대체 | 데이터가 결측치면 T, 아니면 F |
anyNA() | 결측치 확인 | 데이터 전체를 대상으로 확인 |
na.rm() | 결측치 제외 | na.rm = TRUE 설정하면 결측치를 제외하고 분석 |
na.omit() | 결측치 제거 | NA가 있는 행은 제거 |
1. 데이터프레임을 만들어보자
data <- data.frame(class=c("A","B","C","D"),
korean=sample(50:85, 4),
english=sample(85:99,4),
math=sample(55:85,4))
> data
class korean english math
1 A 82 NA 82
2 B 68 98 74
3 C 75 NA 63
4 D 50 99 NA
2. 결측치를 넣어보자
data[1,3] <- NA
data[2,2] <- NA
data[4,4] <- NA
> data
class korean english math
1 A 52 NA 73
2 B NA 88 75
3 C 56 93 70
4 D 76 92 NA
3. 결측치를 확인 해 보자
is.na()
> is.na(data)
class korean english math
[1,] FALSE FALSE TRUE FALSE
[2,] FALSE TRUE FALSE FALSE
[3,] FALSE FALSE FALSE FALSE
[4,] FALSE FALSE FALSE TRUE
> anyNA(data)
[1] TRUE
na.rm()
> mean(data$korean)
[1] NA
> mean(data$korean, na.rm = T)
[1] 61.33333
na.omit() 결측치 제거-결측치가 없는 행만 남음
> data2 <- na.omit(data)
> data2
class korean english math
3 C 56 93 70
> anyNA(data2)
[1] FALSE
9.3.4 이상치 제거
이상치를 결측치로 변경 후 제거
> outlier <- data.frame(sex=c(1,2,2,3,1),
+ class=c("A","B","A","C","A"))
> outlier
sex class
1 1 A
2 2 B
3 2 A
4 3 C
5 1 A
> outlier$sex <- ifelse(outlier$sex==3, NA, outlier$sex)
> outlier
sex class
1 1 A
2 2 B
3 2 A
4 NA C
5 1 A
> outlier <- na.omit(outlier)
> outlier
sex class
1 1 A
2 2 B
3 2 A
5 1 A
10장 데이터 시각화 하기
시각화 하기 package : ggplot2()
install.packages("ggplot2")
library(ggplot2)
요점 :
그래프를 layer (층) 개념으로 이해하자.
1. ggplot(data, aes(x축, y축) +
2. geom_그래프 유형 +
3, labs(그래프 옵션, 상세설정) +
4. 측면보기...
실습1 : iris 종류별 히스토그램 (빈도) 그려보자.
data(iris)
> head(iris)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species
1 5.1 3.5 1.4 0.2 setosa
2 4.9 3.0 1.4 0.2 setosa
3 4.7 3.2 1.3 0.2 setosa
4 4.6 3.1 1.5 0.2 setosa
5 5.0 3.6 1.4 0.2 setosa
6 5.4 3.9 1.7 0.4 setosa
연속형 변수의 범위를 일정구간으로 나누어 각 구간의 값들의 빈도를 그래프로 표현
ggplot(iris, aes(x=Petal.Length, fill=Species))+
geom_histogram()
실습2 : iris 의 종류별 꽃받침 길이의 밀도 -geom_density()
ggplot(iris, aes(x=Sepal.Length, fill=Species))+
geom_density(alpha=0.5)
실습3 : 4분위표 상자그래프 : iris 종류별 꽃받침, 꽃잎 길이 상자도표 _geom_boxplot
ggplot(iris, aes(Species, Petal.Length)) +
geom_boxplot()
ggplot(iris, aes(Species, Sepal.Length, fill=Species)) +
geom_boxplot()
실습4 : 산점도 geom_point()
ggplot(iris, aes(Petal.Length, Petal.Width, color=Species))+
geom_point()
실습5 : bar 그래프 :geom_bar ()
* gapminder dataset 이용
library(gapminder)
> head(gapminder)
# A tibble: 6 × 6
country continent year lifeExp pop gdpPercap
<fct> <fct> <int> <dbl> <int> <dbl>
1 Afghanistan Asia 1952 28.8 8425333 779.
2 Afghanistan Asia 1957 30.3 9240934 821.
3 Afghanistan Asia 1962 32.0 10267083 853.
4 Afghanistan Asia 1967 34.0 11537966 836.
5 Afghanistan Asia 1972 36.1 13079460 740.
6 Afghanistan Asia 1977 38.4 14880372 786.
gap_2007 <- gapminder[gapminder$year==2007,]
gap_2007 <- as.data.frame(gap_2007)
dim(gap_2007)
[1] 142 6
ggplot(gap_2007, aes(continent, fill=continent)) +
geom_bar()
> ggplot(gap_2007, aes(continent, fill=continent)) +
+ geom_bar(show.legend = F)
728x90
반응형