summary(iris)
## Sepal.Length Sepal.Width Petal.Length Petal.Width
## Min. :4.300 Min. :2.000 Min. :1.000 Min. :0.100
## 1st Qu.:5.100 1st Qu.:2.800 1st Qu.:1.600 1st Qu.:0.300
## Median :5.800 Median :3.000 Median :4.350 Median :1.300
## Mean :5.843 Mean :3.057 Mean :3.758 Mean :1.199
## 3rd Qu.:6.400 3rd Qu.:3.300 3rd Qu.:5.100 3rd Qu.:1.800
## Max. :7.900 Max. :4.400 Max. :6.900 Max. :2.500
## Species
## setosa :50
## versicolor:50
## virginica :50
##
##
##
#1번문제
library(hflights)
library(dplyr)
##
## 다음의 패키지를 부착합니다: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
hflights %>% count(Dest) %>% filter(n==max(n)|n==min(n))
## Dest n
## 1 AGS 1
## 2 DAL 9820
#2번문제
table(is.na(hflights$ DepTime))
##
## FALSE TRUE
## 224591 2905
#3번문제
table(hflights$Cancelled)
##
## 0 1
## 224523 2973
#4번문제
data("airquality")
table(is.na(airquality$Ozone))
##
## FALSE TRUE
## 116 37
median(airquality$Ozone,na.rm=TRUE)
## [1] 31.5
airquality$Ozone<-ifelse(is.na(airquality$Ozone),31.5,
airquality$Ozone)
mean(airquality$Ozone)
## [1] 39.55882
#5번 문제
library("ggplot2")
diamonds %>% select(price) %>% arrange(desc(price)) %>%
head(3)
## # A tibble: 3 × 1
## price
## <int>
## 1 18823
## 2 18818
## 3 18806
print(18823)
## [1] 18823
#6번 문제
diamonds %>% group_by(cut,color) %>% summarize(m=mean(price)) %>%
arrange(desc(m)) %>% head(3)
## `summarise()` has grouped output by 'cut'. You can override using the `.groups`
## argument.
## # A tibble: 3 × 3
## # Groups: cut [2]
## cut color m
## <ord> <ord> <dbl>
## 1 Premium J 6295.
## 2 Premium I 5946.
## 3 Very Good I 5256.
#7번 문제
diamonds %>% group_by(cut) %>% summarize(n=n()) %>%
mutate(total=sum(n), pct=n/total*100)
## # A tibble: 5 × 4
## cut n total pct
## <ord> <int> <int> <dbl>
## 1 Fair 1610 53940 2.98
## 2 Good 4906 53940 9.10
## 3 Very Good 12082 53940 22.4
## 4 Premium 13791 53940 25.6
## 5 Ideal 21551 53940 40.0
print(40.0)
## [1] 40
#8번 문제
library(dplyr)
library(gapminder)
str(gapminder)
## tibble [1,704 × 6] (S3: tbl_df/tbl/data.frame)
## $ country : Factor w/ 142 levels "Afghanistan",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ continent: Factor w/ 5 levels "Africa","Americas",..: 3 3 3 3 3 3 3 3 3 3 ...
## $ year : int [1:1704] 1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
## $ lifeExp : num [1:1704] 28.8 30.3 32 34 36.1 ...
## $ pop : int [1:1704] 8425333 9240934 10267083 11537966 13079460 14880372 12881816 13867957 16317921 22227415 ...
## $ gdpPercap: num [1:1704] 779 821 853 836 740 ...
gapminder %>% filter(continent == "Africa"|continent=="Europe") %>%
arrange(desc(pop))
## # A tibble: 984 × 6
## country continent year lifeExp pop gdpPercap
## <fct> <fct> <int> <dbl> <int> <dbl>
## 1 Nigeria Africa 2007 46.9 135031164 2014.
## 2 Nigeria Africa 2002 46.6 119901274 1615.
## 3 Nigeria Africa 1997 47.5 106207839 1625.
## 4 Nigeria Africa 1992 47.5 93364244 1620.
## 5 Germany Europe 2007 79.4 82400996 32170.
## 6 Germany Europe 2002 78.7 82350671 30036.
## 7 Germany Europe 1997 77.3 82011073 27789.
## 8 Nigeria Africa 1987 46.9 81551520 1385.
## 9 Germany Europe 1992 76.1 80597764 26505.
## 10 Egypt Africa 2007 71.3 80264543 5581.
## # ℹ 974 more rows
print(c("Nigeria",2007 ))
## [1] "Nigeria" "2007"
print("Nigeria,2007")
## [1] "Nigeria,2007"