library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.3.2
## Warning: package 'lubridate' was built under R version 4.3.2
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.3     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.4     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(datasets)
library(dplyr)

data("mtcars")
mtcars <- tibble::as_tibble(mtcars)

class(mtcars)
## [1] "tbl_df"     "tbl"        "data.frame"
view(mtcars)
head(mtcars)
## # A tibble: 6 × 11
##     mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
##   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1  21       6   160   110  3.9   2.62  16.5     0     1     4     4
## 2  21       6   160   110  3.9   2.88  17.0     0     1     4     4
## 3  22.8     4   108    93  3.85  2.32  18.6     1     1     4     1
## 4  21.4     6   258   110  3.08  3.22  19.4     1     0     3     1
## 5  18.7     8   360   175  3.15  3.44  17.0     0     0     3     2
## 6  18.1     6   225   105  2.76  3.46  20.2     1     0     3     1
mtcars %>% group_by(cyl) %>% summarise(berat=mean(wt))
## # A tibble: 3 × 2
##     cyl berat
##   <dbl> <dbl>
## 1     4  2.29
## 2     6  3.12
## 3     8  4.00
mtcars %>% arrange(desc(mpg))
## # A tibble: 32 × 11
##      mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
##    <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
##  1  33.9     4  71.1    65  4.22  1.84  19.9     1     1     4     1
##  2  32.4     4  78.7    66  4.08  2.2   19.5     1     1     4     1
##  3  30.4     4  75.7    52  4.93  1.62  18.5     1     1     4     2
##  4  30.4     4  95.1   113  3.77  1.51  16.9     1     1     5     2
##  5  27.3     4  79      66  4.08  1.94  18.9     1     1     4     1
##  6  26       4 120.     91  4.43  2.14  16.7     0     1     5     2
##  7  24.4     4 147.     62  3.69  3.19  20       1     0     4     2
##  8  22.8     4 108      93  3.85  2.32  18.6     1     1     4     1
##  9  22.8     4 141.     95  3.92  3.15  22.9     1     0     4     2
## 10  21.5     4 120.     97  3.7   2.46  20.0     1     0     3     1
## # ℹ 22 more rows
mtcars %>% filter(gear=="4")
## # A tibble: 12 × 11
##      mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb
##    <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
##  1  21       6 160     110  3.9   2.62  16.5     0     1     4     4
##  2  21       6 160     110  3.9   2.88  17.0     0     1     4     4
##  3  22.8     4 108      93  3.85  2.32  18.6     1     1     4     1
##  4  24.4     4 147.     62  3.69  3.19  20       1     0     4     2
##  5  22.8     4 141.     95  3.92  3.15  22.9     1     0     4     2
##  6  19.2     6 168.    123  3.92  3.44  18.3     1     0     4     4
##  7  17.8     6 168.    123  3.92  3.44  18.9     1     0     4     4
##  8  32.4     4  78.7    66  4.08  2.2   19.5     1     1     4     1
##  9  30.4     4  75.7    52  4.93  1.62  18.5     1     1     4     2
## 10  33.9     4  71.1    65  4.22  1.84  19.9     1     1     4     1
## 11  27.3     4  79      66  4.08  1.94  18.9     1     1     4     1
## 12  21.4     4 121     109  4.11  2.78  18.6     1     1     4     2
mtcars %>% select(mpg,disp, wt, qsec)
## # A tibble: 32 × 4
##      mpg  disp    wt  qsec
##    <dbl> <dbl> <dbl> <dbl>
##  1  21    160   2.62  16.5
##  2  21    160   2.88  17.0
##  3  22.8  108   2.32  18.6
##  4  21.4  258   3.22  19.4
##  5  18.7  360   3.44  17.0
##  6  18.1  225   3.46  20.2
##  7  14.3  360   3.57  15.8
##  8  24.4  147.  3.19  20  
##  9  22.8  141.  3.15  22.9
## 10  19.2  168.  3.44  18.3
## # ℹ 22 more rows
mtcars %>% select(-cyl,-hp, -drat)
## # A tibble: 32 × 8
##      mpg  disp    wt  qsec    vs    am  gear  carb
##    <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
##  1  21    160   2.62  16.5     0     1     4     4
##  2  21    160   2.88  17.0     0     1     4     4
##  3  22.8  108   2.32  18.6     1     1     4     1
##  4  21.4  258   3.22  19.4     1     0     3     1
##  5  18.7  360   3.44  17.0     0     0     3     2
##  6  18.1  225   3.46  20.2     1     0     3     1
##  7  14.3  360   3.57  15.8     0     0     3     4
##  8  24.4  147.  3.19  20       1     0     4     2
##  9  22.8  141.  3.15  22.9     1     0     4     2
## 10  19.2  168.  3.44  18.3     1     0     4     4
## # ℹ 22 more rows