`
#exam4) 보스턴 데이터 범죄율 컬럼 top10 중 10번째 범죄율 값으로 1~10위의 범죄율 값을 변경 후
# AGE 변수 80이상의 범죄율 평균 산출
library(MASS)
library(dplyr)
## Warning: 패키지 'dplyr'는 R 버전 4.1.3에서 작성되었습니다
##
## 다음의 패키지를 부착합니다: 'dplyr'
## The following object is masked from 'package:MASS':
##
## select
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
data("Boston")
# Boston 실행 중 dplyr::select() "unused argument" error 발생
# MASS 패키지를 먼저 로딩하고, dplyr 패키지를 나중에 로딩하기
Boston %>% glimpse
## Rows: 506
## Columns: 14
## $ crim <dbl> 0.00632, 0.02731, 0.02729, 0.03237, 0.06905, 0.02985, 0.08829,~
## $ zn <dbl> 18.0, 0.0, 0.0, 0.0, 0.0, 0.0, 12.5, 12.5, 12.5, 12.5, 12.5, 1~
## $ indus <dbl> 2.31, 7.07, 7.07, 2.18, 2.18, 2.18, 7.87, 7.87, 7.87, 7.87, 7.~
## $ chas <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,~
## $ nox <dbl> 0.538, 0.469, 0.469, 0.458, 0.458, 0.458, 0.524, 0.524, 0.524,~
## $ rm <dbl> 6.575, 6.421, 7.185, 6.998, 7.147, 6.430, 6.012, 6.172, 5.631,~
## $ age <dbl> 65.2, 78.9, 61.1, 45.8, 54.2, 58.7, 66.6, 96.1, 100.0, 85.9, 9~
## $ dis <dbl> 4.0900, 4.9671, 4.9671, 6.0622, 6.0622, 6.0622, 5.5605, 5.9505~
## $ rad <int> 1, 2, 2, 3, 3, 3, 5, 5, 5, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4, 4,~
## $ tax <dbl> 296, 242, 242, 222, 222, 222, 311, 311, 311, 311, 311, 311, 31~
## $ ptratio <dbl> 15.3, 17.8, 17.8, 18.7, 18.7, 18.7, 15.2, 15.2, 15.2, 15.2, 15~
## $ black <dbl> 396.90, 396.90, 392.83, 394.63, 396.90, 394.12, 395.60, 396.90~
## $ lstat <dbl> 4.98, 9.14, 4.03, 2.94, 5.33, 5.21, 12.43, 19.15, 29.93, 17.10~
## $ medv <dbl> 24.0, 21.6, 34.7, 33.4, 36.2, 28.7, 22.9, 27.1, 16.5, 18.9, 15~
# crim 범죄률 변수
boston1<-Boston %>% arrange(desc(crim))
boston1 %>% head
## crim zn indus chas nox rm age dis rad tax ptratio black lstat
## 1 88.9762 0 18.1 0 0.671 6.968 91.9 1.4165 24 666 20.2 396.90 17.21
## 2 73.5341 0 18.1 0 0.679 5.957 100.0 1.8026 24 666 20.2 16.45 20.62
## 3 67.9208 0 18.1 0 0.693 5.683 100.0 1.4254 24 666 20.2 384.97 22.98
## 4 51.1358 0 18.1 0 0.597 5.757 100.0 1.4130 24 666 20.2 2.60 10.11
## 5 45.7461 0 18.1 0 0.693 4.519 100.0 1.6582 24 666 20.2 88.27 36.98
## 6 41.5292 0 18.1 0 0.693 5.531 85.4 1.6074 24 666 20.2 329.46 27.38
## medv
## 1 10.4
## 2 8.8
## 3 5.0
## 4 15.0
## 5 7.0
## 6 8.5
boston1$crim[10]
## [1] 25.9406
# crip(typing error)
boston1$crim[1:10]<-25.9406
boston1 %>% head(10)
## crim zn indus chas nox rm age dis rad tax ptratio black lstat
## 1 25.9406 0 18.1 0 0.671 6.968 91.9 1.4165 24 666 20.2 396.90 17.21
## 2 25.9406 0 18.1 0 0.679 5.957 100.0 1.8026 24 666 20.2 16.45 20.62
## 3 25.9406 0 18.1 0 0.693 5.683 100.0 1.4254 24 666 20.2 384.97 22.98
## 4 25.9406 0 18.1 0 0.597 5.757 100.0 1.4130 24 666 20.2 2.60 10.11
## 5 25.9406 0 18.1 0 0.693 4.519 100.0 1.6582 24 666 20.2 88.27 36.98
## 6 25.9406 0 18.1 0 0.693 5.531 85.4 1.6074 24 666 20.2 329.46 27.38
## 7 25.9406 0 18.1 0 0.693 5.453 100.0 1.4896 24 666 20.2 396.90 30.59
## 8 25.9406 0 18.1 0 0.679 6.202 78.7 1.8629 24 666 20.2 18.82 14.52
## 9 25.9406 0 18.1 0 0.597 5.155 100.0 1.5894 24 666 20.2 210.97 20.08
## 10 25.9406 0 18.1 0 0.679 5.304 89.1 1.6475 24 666 20.2 127.36 26.64
## medv
## 1 10.4
## 2 8.8
## 3 5.0
## 4 15.0
## 5 7.0
## 6 8.5
## 7 5.0
## 8 10.9
## 9 16.3
## 10 10.4
boston1 %>% filter(age>=80) %>% select(crim) %>% summarize(m=mean(crim))->df
print(df[[1]])
## [1] 5.759387