rm(list=ls())
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
head(iris, 10)
##    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
## 7           4.6         3.4          1.4         0.3  setosa
## 8           5.0         3.4          1.5         0.2  setosa
## 9           4.4         2.9          1.4         0.2  setosa
## 10          4.9         3.1          1.5         0.1  setosa
tail(iris)
##     Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
## 145          6.7         3.3          5.7         2.5 virginica
## 146          6.7         3.0          5.2         2.3 virginica
## 147          6.3         2.5          5.0         1.9 virginica
## 148          6.5         3.0          5.2         2.0 virginica
## 149          6.2         3.4          5.4         2.3 virginica
## 150          5.9         3.0          5.1         1.8 virginica
tail(iris, 10)
##     Sepal.Length Sepal.Width Petal.Length Petal.Width   Species
## 141          6.7         3.1          5.6         2.4 virginica
## 142          6.9         3.1          5.1         2.3 virginica
## 143          5.8         2.7          5.1         1.9 virginica
## 144          6.8         3.2          5.9         2.3 virginica
## 145          6.7         3.3          5.7         2.5 virginica
## 146          6.7         3.0          5.2         2.3 virginica
## 147          6.3         2.5          5.0         1.9 virginica
## 148          6.5         3.0          5.2         2.0 virginica
## 149          6.2         3.4          5.4         2.3 virginica
## 150          5.9         3.0          5.1         1.8 virginica
str(iris)
## 'data.frame':    150 obs. of  5 variables:
##  $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
##  $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
##  $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
##  $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
##  $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
View(iris)
dim(iris)
## [1] 150   5
length(iris)
## [1] 5
length(iris$Sepal.Length)
## [1] 150
iris$Sepal.Length
##   [1] 5.1 4.9 4.7 4.6 5.0 5.4 4.6 5.0 4.4 4.9 5.4 4.8 4.8 4.3 5.8 5.7 5.4 5.1
##  [19] 5.7 5.1 5.4 5.1 4.6 5.1 4.8 5.0 5.0 5.2 5.2 4.7 4.8 5.4 5.2 5.5 4.9 5.0
##  [37] 5.5 4.9 4.4 5.1 5.0 4.5 4.4 5.0 5.1 4.8 5.1 4.6 5.3 5.0 7.0 6.4 6.9 5.5
##  [55] 6.5 5.7 6.3 4.9 6.6 5.2 5.0 5.9 6.0 6.1 5.6 6.7 5.6 5.8 6.2 5.6 5.9 6.1
##  [73] 6.3 6.1 6.4 6.6 6.8 6.7 6.0 5.7 5.5 5.5 5.8 6.0 5.4 6.0 6.7 6.3 5.6 5.5
##  [91] 5.5 6.1 5.8 5.0 5.6 5.7 5.7 6.2 5.1 5.7 6.3 5.8 7.1 6.3 6.5 7.6 4.9 7.3
## [109] 6.7 7.2 6.5 6.4 6.8 5.7 5.8 6.4 6.5 7.7 7.7 6.0 6.9 5.6 7.7 6.3 6.7 7.2
## [127] 6.2 6.1 6.4 7.2 7.4 7.9 6.4 6.3 6.1 7.7 6.3 6.4 6.0 6.9 6.7 6.9 5.8 6.8
## [145] 6.7 6.7 6.3 6.5 6.2 5.9
iris[,1]
##   [1] 5.1 4.9 4.7 4.6 5.0 5.4 4.6 5.0 4.4 4.9 5.4 4.8 4.8 4.3 5.8 5.7 5.4 5.1
##  [19] 5.7 5.1 5.4 5.1 4.6 5.1 4.8 5.0 5.0 5.2 5.2 4.7 4.8 5.4 5.2 5.5 4.9 5.0
##  [37] 5.5 4.9 4.4 5.1 5.0 4.5 4.4 5.0 5.1 4.8 5.1 4.6 5.3 5.0 7.0 6.4 6.9 5.5
##  [55] 6.5 5.7 6.3 4.9 6.6 5.2 5.0 5.9 6.0 6.1 5.6 6.7 5.6 5.8 6.2 5.6 5.9 6.1
##  [73] 6.3 6.1 6.4 6.6 6.8 6.7 6.0 5.7 5.5 5.5 5.8 6.0 5.4 6.0 6.7 6.3 5.6 5.5
##  [91] 5.5 6.1 5.8 5.0 5.6 5.7 5.7 6.2 5.1 5.7 6.3 5.8 7.1 6.3 6.5 7.6 4.9 7.3
## [109] 6.7 7.2 6.5 6.4 6.8 5.7 5.8 6.4 6.5 7.7 7.7 6.0 6.9 5.6 7.7 6.3 6.7 7.2
## [127] 6.2 6.1 6.4 7.2 7.4 7.9 6.4 6.3 6.1 7.7 6.3 6.4 6.0 6.9 6.7 6.9 5.8 6.8
## [145] 6.7 6.7 6.3 6.5 6.2 5.9
iris[1,]
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
class(iris)
## [1] "data.frame"
class(iris$Petal.Length)
## [1] "numeric"
ls(iris)
## [1] "Petal.Length" "Petal.Width"  "Sepal.Length" "Sepal.Width"  "Species"
mean(mtcars$mpg, na.rm=TRUE)
## [1] 20.09062
var(mtcars$mpg, na.rm=TRUE)
## [1] 36.3241
sd(mtcars$mpg, na.rm=TRUE)
## [1] 6.026948
sum(mtcars$mpg, na.rm=TRUE)
## [1] 642.9
range(mtcars$mpg, na.rm=TRUE)
## [1] 10.4 33.9
max(mtcars$mpg, na.rm=TRUE)
## [1] 33.9
min(mtcars$mpg, na.rm=TRUE)
## [1] 10.4
quantile(mtcars$mpg, na.rm=TRUE)
##     0%    25%    50%    75%   100% 
## 10.400 15.425 19.200 22.800 33.900
IQR(mtcars$mpg, na.rm=TRUE)
## [1] 7.375
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  
##                 
##                 
## 
table(iris$Petal.Length)
## 
##   1 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.9   3 3.3 3.5 3.6 3.7 3.8 3.9   4 4.1 4.2 4.3 
##   1   1   2   7  13  13   7   4   2   1   2   2   1   1   1   3   5   3   4   2 
## 4.4 4.5 4.6 4.7 4.8 4.9   5 5.1 5.2 5.3 5.4 5.5 5.6 5.7 5.8 5.9   6 6.1 6.3 6.4 
##   4   8   3   5   4   5   4   8   2   2   2   3   6   3   3   2   2   3   1   1 
## 6.6 6.7 6.9 
##   1   2   1
df<-read.csv("Data1.csv")
str(df)
## 'data.frame':    1925 obs. of  26 variables:
##  $ Q1       : int  4 4 4 5 4 4 4 4 4 4 ...
##  $ Q2       : int  4 4 4 4 4 4 2 2 4 4 ...
##  $ Q3       : int  2 4 4 4 4 4 4 4 4 2 ...
##  $ Q4       : int  3 4 4 4 4 4 4 4 4 2 ...
##  $ Q5       : int  4 4 2 4 4 4 4 4 2 4 ...
##  $ Q6       : int  2 3 4 4 4 4 4 4 1 2 ...
##  $ Q7       : int  2 2 4 4 4 4 4 4 3 4 ...
##  $ Q8       : int  4 4 4 4 4 4 5 5 2 2 ...
##  $ Q9       : int  4 4 4 4 2 4 5 5 3 4 ...
##  $ Q10      : int  4 4 2 4 4 4 5 5 2 4 ...
##  $ Q11      : int  4 4 4 4 4 4 5 5 4 4 ...
##  $ Q12      : int  4 4 4 4 4 4 5 5 3 4 ...
##  $ Q13      : int  4 4 4 4 4 4 5 5 4 4 ...
##  $ Q14      : int  4 4 4 4 4 4 5 5 5 4 ...
##  $ Q15      : int  4 4 3 4 4 4 4 2 3 4 ...
##  $ Q16      : int  4 4 4 4 4 4 5 2 4 4 ...
##  $ Q17      : int  4 3 4 4 4 4 2 2 4 4 ...
##  $ Q18      : int  4 4 4 4 4 4 4 4 4 4 ...
##  $ Q19      : int  4 2 4 4 4 4 4 2 4 2 ...
##  $ Q20      : int  4 1 3 4 4 4 4 2 4 2 ...
##  $ Gender1  : int  0 0 0 0 0 0 0 0 1 0 ...
##  $ EDU1     : int  1 1 2 1 2 1 1 1 4 3 ...
##  $ BF       : num  3.4 4 3.6 4.2 4 4 3.6 3.6 3.6 3.2 ...
##  $ BM       : num  3.2 3.4 3.6 4 3.6 4 4.6 4.6 2.2 3.2 ...
##  $ Happiness: num  4 4 3.8 4 4 4 4.8 4.4 3.8 4 ...
##  $ Peace    : num  4 2.8 3.8 4 4 4 3.8 2.4 4 3.2 ...
df$Gender1<-factor(df$Gender1)
str(df)
## 'data.frame':    1925 obs. of  26 variables:
##  $ Q1       : int  4 4 4 5 4 4 4 4 4 4 ...
##  $ Q2       : int  4 4 4 4 4 4 2 2 4 4 ...
##  $ Q3       : int  2 4 4 4 4 4 4 4 4 2 ...
##  $ Q4       : int  3 4 4 4 4 4 4 4 4 2 ...
##  $ Q5       : int  4 4 2 4 4 4 4 4 2 4 ...
##  $ Q6       : int  2 3 4 4 4 4 4 4 1 2 ...
##  $ Q7       : int  2 2 4 4 4 4 4 4 3 4 ...
##  $ Q8       : int  4 4 4 4 4 4 5 5 2 2 ...
##  $ Q9       : int  4 4 4 4 2 4 5 5 3 4 ...
##  $ Q10      : int  4 4 2 4 4 4 5 5 2 4 ...
##  $ Q11      : int  4 4 4 4 4 4 5 5 4 4 ...
##  $ Q12      : int  4 4 4 4 4 4 5 5 3 4 ...
##  $ Q13      : int  4 4 4 4 4 4 5 5 4 4 ...
##  $ Q14      : int  4 4 4 4 4 4 5 5 5 4 ...
##  $ Q15      : int  4 4 3 4 4 4 4 2 3 4 ...
##  $ Q16      : int  4 4 4 4 4 4 5 2 4 4 ...
##  $ Q17      : int  4 3 4 4 4 4 2 2 4 4 ...
##  $ Q18      : int  4 4 4 4 4 4 4 4 4 4 ...
##  $ Q19      : int  4 2 4 4 4 4 4 2 4 2 ...
##  $ Q20      : int  4 1 3 4 4 4 4 2 4 2 ...
##  $ Gender1  : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 2 1 ...
##  $ EDU1     : int  1 1 2 1 2 1 1 1 4 3 ...
##  $ BF       : num  3.4 4 3.6 4.2 4 4 3.6 3.6 3.6 3.2 ...
##  $ BM       : num  3.2 3.4 3.6 4 3.6 4 4.6 4.6 2.2 3.2 ...
##  $ Happiness: num  4 4 3.8 4 4 4 4.8 4.4 3.8 4 ...
##  $ Peace    : num  4 2.8 3.8 4 4 4 3.8 2.4 4 3.2 ...
df$EDU1<-factor(df$EDU1)
levels(df$Gender1)
## [1] "0" "1"
levels(df$EDU1)
## [1] "1" "2" "3" "4"
#install.packages("plyr")
library(plyr)
df$Gender1<-revalue(df$Gender1, replace = c("0"="female", "1"="male"))
df$EDU1<-revalue(df$EDU1, replace = c('1'='high', '2'='university', '3'='graduate', '4'="phd"))
table(df$Gender1)
## 
## female   male 
##   1136    789
table(df$EDU1)
## 
##       high university   graduate        phd 
##        233        472       1022        198
a<-table(df$Gender1)
b<-table(df$EDU1)
round(prop.table(a), 2)
## 
## female   male 
##   0.59   0.41
round(prop.table(b), 3)
## 
##       high university   graduate        phd 
##      0.121      0.245      0.531      0.103
e<-table(df$Gender1, df$EDU1)
prop.table(e)
##         
##                high university   graduate        phd
##   female 0.09454545 0.14389610 0.30233766 0.04935065
##   male   0.02649351 0.10129870 0.22857143 0.05350649
round(prop.table(e), 2)
##         
##          high university graduate  phd
##   female 0.09       0.14     0.30 0.05
##   male   0.03       0.10     0.23 0.05
prop.table(e, 1)
##         
##                high university   graduate        phd
##   female 0.16021127 0.24383803 0.51232394 0.08362676
##   male   0.06463878 0.24714829 0.55766793 0.13054499
prop.table(e, 2)
##         
##               high university  graduate       phd
##   female 0.7811159  0.5868644 0.5694716 0.4797980
##   male   0.2188841  0.4131356 0.4305284 0.5202020
library(dplyr)
## 
## 다음의 패키지를 부착합니다: 'dplyr'
## The following objects are masked from 'package:plyr':
## 
##     arrange, count, desc, failwith, id, mutate, rename, summarise,
##     summarize
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggplot2)
data("diamonds")
head(diamonds)
## # A tibble: 6 × 10
##   carat cut       color clarity depth table price     x     y     z
##   <dbl> <ord>     <ord> <ord>   <dbl> <dbl> <int> <dbl> <dbl> <dbl>
## 1  0.23 Ideal     E     SI2      61.5    55   326  3.95  3.98  2.43
## 2  0.21 Premium   E     SI1      59.8    61   326  3.89  3.84  2.31
## 3  0.23 Good      E     VS1      56.9    65   327  4.05  4.07  2.31
## 4  0.29 Premium   I     VS2      62.4    58   334  4.2   4.23  2.63
## 5  0.31 Good      J     SI2      63.3    58   335  4.34  4.35  2.75
## 6  0.24 Very Good J     VVS2     62.8    57   336  3.94  3.96  2.48
diamonds1<-diamonds %>% rename(c=clarity, p=price)
head(diamonds1)
## # A tibble: 6 × 10
##   carat cut       color c     depth table     p     x     y     z
##   <dbl> <ord>     <ord> <ord> <dbl> <dbl> <int> <dbl> <dbl> <dbl>
## 1  0.23 Ideal     E     SI2    61.5    55   326  3.95  3.98  2.43
## 2  0.21 Premium   E     SI1    59.8    61   326  3.89  3.84  2.31
## 3  0.23 Good      E     VS1    56.9    65   327  4.05  4.07  2.31
## 4  0.29 Premium   I     VS2    62.4    58   334  4.2   4.23  2.63
## 5  0.31 Good      J     SI2    63.3    58   335  4.34  4.35  2.75
## 6  0.24 Very Good J     VVS2   62.8    57   336  3.94  3.96  2.48
count(diamonds, color)
## # A tibble: 7 × 2
##   color     n
##   <ord> <int>
## 1 D      6775
## 2 E      9797
## 3 F      9542
## 4 G     11292
## 5 H      8304
## 6 I      5422
## 7 J      2808
table(diamonds$color)
## 
##     D     E     F     G     H     I     J 
##  6775  9797  9542 11292  8304  5422  2808
class(count(diamonds, color))
## [1] "tbl_df"     "tbl"        "data.frame"
df1<-diamonds %>% select(carat, price)
head(df1, 3)
## # A tibble: 3 × 2
##   carat price
##   <dbl> <int>
## 1  0.23   326
## 2  0.21   326
## 3  0.23   327
ls(diamonds)
##  [1] "carat"   "clarity" "color"   "cut"     "depth"   "price"   "table"  
##  [8] "x"       "y"       "z"
diamonds %>% select(-1:-5, -7)
## # A tibble: 53,940 × 4
##    table     x     y     z
##    <dbl> <dbl> <dbl> <dbl>
##  1    55  3.95  3.98  2.43
##  2    61  3.89  3.84  2.31
##  3    65  4.05  4.07  2.31
##  4    58  4.2   4.23  2.63
##  5    58  4.34  4.35  2.75
##  6    57  3.94  3.96  2.48
##  7    57  3.95  3.98  2.47
##  8    55  4.07  4.11  2.53
##  9    61  3.87  3.78  2.49
## 10    61  4     4.05  2.39
## # … with 53,930 more rows
#diamonds %>% top_n(x)
#top_n(diamonds, )

diamonds %>% filter(cut=='Good') %>% head(3)
## # A tibble: 3 × 10
##   carat cut   color clarity depth table price     x     y     z
##   <dbl> <ord> <ord> <ord>   <dbl> <dbl> <int> <dbl> <dbl> <dbl>
## 1  0.23 Good  E     VS1      56.9    65   327  4.05  4.07  2.31
## 2  0.31 Good  J     SI2      63.3    58   335  4.34  4.35  2.75
## 3  0.3  Good  J     SI1      64      55   339  4.25  4.28  2.73
max(diamonds$price)
## [1] 18823
diamonds %>% filter(price==max(price))
## # A tibble: 1 × 10
##   carat cut     color clarity depth table price     x     y     z
##   <dbl> <ord>   <ord> <ord>   <dbl> <dbl> <int> <dbl> <dbl> <dbl>
## 1  2.29 Premium I     VS2      60.8    60 18823   8.5  8.47  5.16
diamonds %>% filter(cut!="Premium") %>% head(3)
## # A tibble: 3 × 10
##   carat cut   color clarity depth table price     x     y     z
##   <dbl> <ord> <ord> <ord>   <dbl> <dbl> <int> <dbl> <dbl> <dbl>
## 1  0.23 Ideal E     SI2      61.5    55   326  3.95  3.98  2.43
## 2  0.23 Good  E     VS1      56.9    65   327  4.05  4.07  2.31
## 3  0.31 Good  J     SI2      63.3    58   335  4.34  4.35  2.75
diamonds %>% filter(price>=1000) %>% head(3)
## # A tibble: 3 × 10
##   carat cut   color clarity depth table price     x     y     z
##   <dbl> <ord> <ord> <ord>   <dbl> <dbl> <int> <dbl> <dbl> <dbl>
## 1  0.7  Ideal E     SI1      62.5    57  2757  5.7   5.72  3.57
## 2  0.86 Fair  E     SI2      55.1    69  2757  6.45  6.33  3.52
## 3  0.7  Ideal G     VS2      61.6    56  2757  5.7   5.67  3.5
diamonds %>% filter(price!=1000) %>% head(3)
## # A tibble: 3 × 10
##   carat cut     color clarity depth table price     x     y     z
##   <dbl> <ord>   <ord> <ord>   <dbl> <dbl> <int> <dbl> <dbl> <dbl>
## 1  0.23 Ideal   E     SI2      61.5    55   326  3.95  3.98  2.43
## 2  0.21 Premium E     SI1      59.8    61   326  3.89  3.84  2.31
## 3  0.23 Good    E     VS1      56.9    65   327  4.05  4.07  2.31
diamonds %>% filter(price==1000) %>% head(3)
## # A tibble: 3 × 10
##   carat cut       color clarity depth table price     x     y     z
##   <dbl> <ord>     <ord> <ord>   <dbl> <dbl> <int> <dbl> <dbl> <dbl>
## 1  0.38 Very Good E     VVS2     61.8    56  1000  4.66  4.68  2.88
## 2  0.39 Very Good F     VS1      57.1    61  1000  4.86  4.91  2.79
## 3  0.38 Very Good E     VS1      61.5    58  1000  4.64  4.69  2.87
diamonds %>% filter(price!=1000&cut=="Ideal") %>% head(3)
## # A tibble: 3 × 10
##   carat cut   color clarity depth table price     x     y     z
##   <dbl> <ord> <ord> <ord>   <dbl> <dbl> <int> <dbl> <dbl> <dbl>
## 1  0.23 Ideal E     SI2      61.5    55   326  3.95  3.98  2.43
## 2  0.23 Ideal J     VS1      62.8    56   340  3.93  3.9   2.46
## 3  0.31 Ideal J     SI2      62.2    54   344  4.35  4.37  2.71
diamonds %>% filter(price!=1000&cut=="Ideal"&color=='E') %>% head(3)
## # A tibble: 3 × 10
##   carat cut   color clarity depth table price     x     y     z
##   <dbl> <ord> <ord> <ord>   <dbl> <dbl> <int> <dbl> <dbl> <dbl>
## 1  0.23 Ideal E     SI2      61.5    55   326  3.95  3.98  2.43
## 2  0.26 Ideal E     VVS2     62.9    58   554  4.02  4.06  2.54
## 3  0.7  Ideal E     SI1      62.5    57  2757  5.7   5.72  3.57
diamonds %>% filter(price!=1000&cut=="Ideal") %>% head(3)
## # A tibble: 3 × 10
##   carat cut   color clarity depth table price     x     y     z
##   <dbl> <ord> <ord> <ord>   <dbl> <dbl> <int> <dbl> <dbl> <dbl>
## 1  0.23 Ideal E     SI2      61.5    55   326  3.95  3.98  2.43
## 2  0.23 Ideal J     VS1      62.8    56   340  3.93  3.9   2.46
## 3  0.31 Ideal J     SI2      62.2    54   344  4.35  4.37  2.71
diamonds %>% filter(carat<1|carat>5) %>% head(3)
## # A tibble: 3 × 10
##   carat cut     color clarity depth table price     x     y     z
##   <dbl> <ord>   <ord> <ord>   <dbl> <dbl> <int> <dbl> <dbl> <dbl>
## 1  0.23 Ideal   E     SI2      61.5    55   326  3.95  3.98  2.43
## 2  0.21 Premium E     SI1      59.8    61   326  3.89  3.84  2.31
## 3  0.23 Good    E     VS1      56.9    65   327  4.05  4.07  2.31
diamonds %>% filter(cut%in%c("Ideal", "Good")) %>% head(3)
## # A tibble: 3 × 10
##   carat cut   color clarity depth table price     x     y     z
##   <dbl> <ord> <ord> <ord>   <dbl> <dbl> <int> <dbl> <dbl> <dbl>
## 1  0.23 Ideal E     SI2      61.5    55   326  3.95  3.98  2.43
## 2  0.23 Good  E     VS1      56.9    65   327  4.05  4.07  2.31
## 3  0.31 Good  J     SI2      63.3    58   335  4.34  4.35  2.75
diamonds %>% select(carat, depth, price) %>% 
  filter(depth==max(depth)|price==min(price))
## # A tibble: 4 × 3
##   carat depth price
##   <dbl> <dbl> <int>
## 1  0.23  61.5   326
## 2  0.21  59.8   326
## 3  0.5   79    2579
## 4  0.5   79    2579
diamonds %>% mutate(Ratio=price/carat, Double=Ratio*2) %>% head(3)
## # A tibble: 3 × 12
##   carat cut     color clarity depth table price     x     y     z Ratio Double
##   <dbl> <ord>   <ord> <ord>   <dbl> <dbl> <int> <dbl> <dbl> <dbl> <dbl>  <dbl>
## 1  0.23 Ideal   E     SI2      61.5    55   326  3.95  3.98  2.43 1417.  2835.
## 2  0.21 Premium E     SI1      59.8    61   326  3.89  3.84  2.31 1552.  3105.
## 3  0.23 Good    E     VS1      56.9    65   327  4.05  4.07  2.31 1422.  2843.