getwd()
## [1] "C:/data"
setwd("c:/data")
df<-read.csv("Data1.csv")
library(dplyr)
## Warning: 패키지 'dplyr'는 R 버전 4.2.3에서 작성되었습니다
##
## 다음의 패키지를 부착합니다: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(ggplot2)
## Warning: 패키지 'ggplot2'는 R 버전 4.2.3에서 작성되었습니다
data(midwest)
head(midwest)
## # A tibble: 6 × 28
## PID county state area poptotal popdens…¹ popwh…² popbl…³ popam…⁴ popas…⁵
## <int> <chr> <chr> <dbl> <int> <dbl> <int> <int> <int> <int>
## 1 561 ADAMS IL 0.052 66090 1271. 63917 1702 98 249
## 2 562 ALEXANDER IL 0.014 10626 759 7054 3496 19 48
## 3 563 BOND IL 0.022 14991 681. 14477 429 35 16
## 4 564 BOONE IL 0.017 30806 1812. 29344 127 46 150
## 5 565 BROWN IL 0.018 5836 324. 5264 547 14 5
## 6 566 BUREAU IL 0.05 35688 714. 35157 50 65 195
## # … with 18 more variables: popother <int>, percwhite <dbl>, percblack <dbl>,
## # percamerindan <dbl>, percasian <dbl>, percother <dbl>, popadults <int>,
## # perchsd <dbl>, percollege <dbl>, percprof <dbl>, poppovertyknown <int>,
## # percpovertyknown <dbl>, percbelowpoverty <dbl>, percchildbelowpovert <dbl>,
## # percadultpoverty <dbl>, percelderlypoverty <dbl>, inmetro <int>,
## # category <chr>, and abbreviated variable names ¹popdensity, ²popwhite,
## # ³popblack, ⁴popamerindian, ⁵popasian
tail(midwest)
## # A tibble: 6 × 28
## PID county state area poptotal popden…¹ popwh…² popbl…³ popam…⁴ popas…⁵
## <int> <chr> <chr> <dbl> <int> <dbl> <int> <int> <int> <int>
## 1 3047 WASHINGTON WI 0.025 95328 3813. 94465 125 208 337
## 2 3048 WAUKESHA WI 0.034 304715 8962. 298313 1096 672 2699
## 3 3049 WAUPACA WI 0.045 46104 1025. 45695 22 125 92
## 4 3050 WAUSHARA WI 0.037 19385 524. 19094 29 70 43
## 5 3051 WINNEBAGO WI 0.035 140320 4009. 136822 697 685 1728
## 6 3052 WOOD WI 0.048 73605 1533. 72157 90 481 722
## # … with 18 more variables: popother <int>, percwhite <dbl>, percblack <dbl>,
## # percamerindan <dbl>, percasian <dbl>, percother <dbl>, popadults <int>,
## # perchsd <dbl>, percollege <dbl>, percprof <dbl>, poppovertyknown <int>,
## # percpovertyknown <dbl>, percbelowpoverty <dbl>, percchildbelowpovert <dbl>,
## # percadultpoverty <dbl>, percelderlypoverty <dbl>, inmetro <int>,
## # category <chr>, and abbreviated variable names ¹popdensity, ²popwhite,
## # ³popblack, ⁴popamerindian, ⁵popasian
dim(midwest)
## [1] 437 28
midwest %>% head %>% dim
## [1] 6 28
df1<-midwest %>% select(county,state,poptotal,popasian)
midwest %>% select(county,state,poptotal,popasian)
## # A tibble: 437 × 4
## county state poptotal popasian
## <chr> <chr> <int> <int>
## 1 ADAMS IL 66090 249
## 2 ALEXANDER IL 10626 48
## 3 BOND IL 14991 16
## 4 BOONE IL 30806 150
## 5 BROWN IL 5836 5
## 6 BUREAU IL 35688 195
## 7 CALHOUN IL 5322 15
## 8 CARROLL IL 16805 61
## 9 CASS IL 13437 23
## 10 CHAMPAIGN IL 173025 8033
## # … with 427 more rows
midwest1<-midwest %>% rename(asian=popasian,total=poptotal)
head(midwest1,3)
## # A tibble: 3 × 28
## PID county state area total popde…¹ popwh…² popbl…³ popam…⁴ asian popot…⁵
## <int> <chr> <chr> <dbl> <int> <dbl> <int> <int> <int> <int> <int>
## 1 561 ADAMS IL 0.052 66090 1271. 63917 1702 98 249 124
## 2 562 ALEXAND… IL 0.014 10626 759 7054 3496 19 48 9
## 3 563 BOND IL 0.022 14991 681. 14477 429 35 16 34
## # … with 17 more variables: percwhite <dbl>, percblack <dbl>,
## # percamerindan <dbl>, percasian <dbl>, percother <dbl>, popadults <int>,
## # perchsd <dbl>, percollege <dbl>, percprof <dbl>, poppovertyknown <int>,
## # percpovertyknown <dbl>, percbelowpoverty <dbl>, percchildbelowpovert <dbl>,
## # percadultpoverty <dbl>, percelderlypoverty <dbl>, inmetro <int>,
## # category <chr>, and abbreviated variable names ¹popdensity, ²popwhite,
## # ³popblack, ⁴popamerindian, ⁵popother
pct_asian<-(midwest1$asian/midwest1$total)*100
totasian_mean<-mean(pct_asian)
midwest1$mean<-ifelse(pct_asian>totasian_mean, "large", "small")
table(midwest1$mean)
##
## large small
## 119 318