# select single column by nameproduct_dat <- budget %>%select(product) # select single column by numberproduct_dat <- budget %>%select(2) # select single column by numberproduct_dat <- budget %>%select(2,3)
# A tibble: 8 × 3
region product sales_2019
<chr> <chr> <dbl>
1 North widgets 2129
2 North gadgets 723
3 South widgets 1123
4 South gadgets 2022
5 East widgets -728
6 East gadgets -423
7 West widgets 633
8 West gadgets 1204
# select all rows where sales_2019 was more than 100budget %>%filter(sales_2019 >100)
# A tibble: 6 × 8
region product sales_2019 sales_2020 expenses_2019 expenses_…¹ satis…² satis…³
<chr> <chr> <dbl> <dbl> <dbl> <dbl> <chr> <chr>
1 North widgets 2129 -517 822 -897 high very h…
2 North gadgets 723 77 1037 1115 very h… very h…
3 South widgets 1123 -1450 1004 672 high neutral
4 South gadgets 2022 -945 -610 200 low low
5 West widgets 633 790 783 -315 neutral neutral
6 West gadgets 1204 426 433 -136 low low
# … with abbreviated variable names ¹expenses_2020, ²satisfaction_2019,
# ³satisfaction_2020
# everything but the Northbudget %>%filter(region !="North")
# A tibble: 6 × 8
region product sales_2019 sales_2020 expenses_2019 expenses_…¹ satis…² satis…³
<chr> <chr> <dbl> <dbl> <dbl> <dbl> <chr> <chr>
1 South widgets 1123 -1450 1004 672 high neutral
2 South gadgets 2022 -945 -610 200 low low
3 East widgets -728 -51 -801 -342 very l… very l…
4 East gadgets -423 -354 94 2036 neutral high
5 West widgets 633 790 783 -315 neutral neutral
6 West gadgets 1204 426 433 -136 low low
# … with abbreviated variable names ¹expenses_2020, ²satisfaction_2019,
# ³satisfaction_2020
# regions and products with profit in both 2019 and 2020profit_both <- budget %>%filter( sales_2019 > expenses_2019, sales_2020 > expenses_2020 )# the same as above, using & instead of a commaprofit_both <- budget %>%filter( sales_2019 > expenses_2019 & sales_2020 > expenses_2020 )# regions and products with profit in 2019 or 2020profit_either <- budget %>%filter( sales_2019 > expenses_2019 | sales_2020 > expenses_2020 )# 2020 profit greater than 1000profit_1000 <- budget %>%filter(sales_2020 - expenses_2020 >1000)
in
# retain any rows where region is north or south, and where product equals widgetbudget %>%filter(region %in%c("North", "South"), product =="widgets")
# A tibble: 2 × 8
region product sales_2019 sales_2020 expenses_2019 expenses_…¹ satis…² satis…³
<chr> <chr> <dbl> <dbl> <dbl> <dbl> <chr> <chr>
1 North widgets 2129 -517 822 -897 high very h…
2 South widgets 1123 -1450 1004 672 high neutral
# … with abbreviated variable names ¹expenses_2020, ²satisfaction_2019,
# ³satisfaction_2020
# retain any rows where the region is not east or west, and where the product does not equal gadgetsbudget %>%filter(!region %in%c("East", "West"), product !="gadgets")
# A tibble: 2 × 8
region product sales_2019 sales_2020 expenses_2019 expenses_…¹ satis…² satis…³
<chr> <chr> <dbl> <dbl> <dbl> <dbl> <chr> <chr>
1 North widgets 2129 -517 822 -897 high very h…
2 South widgets 1123 -1450 1004 672 high neutral
# … with abbreviated variable names ¹expenses_2020, ²satisfaction_2019,
# ³satisfaction_2020
a <-c(1,2,3,4,5)6%in% a
[1] FALSE
1%in% a
[1] TRUE
if(1%in% a ==TRUE) {"yes"}
[1] "yes"
if(6%in% a ==FALSE) {"yes"}
[1] "yes"
letters
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
[20] "t" "u" "v" "w" "x" "y" "z"
LETTERS
[1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S"
[20] "T" "U" "V" "W" "X" "Y" "Z"
which(letters %in%"g", arr.ind =TRUE)
[1] 7
arrange
# arranging the table # first by product in alphabetical order# then by "region" in reverse alphabetical orderbudget %>%arrange(product, desc(region))
# A tibble: 8 × 8
region product sales_2019 sales_2020 expenses_2019 expenses_…¹ satis…² satis…³
<chr> <chr> <dbl> <dbl> <dbl> <dbl> <chr> <chr>
1 West gadgets 1204 426 433 -136 low low
2 South gadgets 2022 -945 -610 200 low low
3 North gadgets 723 77 1037 1115 very h… very h…
4 East gadgets -423 -354 94 2036 neutral high
5 West widgets 633 790 783 -315 neutral neutral
6 South widgets 1123 -1450 1004 672 high neutral
7 North widgets 2129 -517 822 -897 high very h…
8 East widgets -728 -51 -801 -342 very l… very l…
# … with abbreviated variable names ¹expenses_2020, ²satisfaction_2019,
# ³satisfaction_2020
budget %>%arrange(product, region)
# A tibble: 8 × 8
region product sales_2019 sales_2020 expenses_2019 expenses_…¹ satis…² satis…³
<chr> <chr> <dbl> <dbl> <dbl> <dbl> <chr> <chr>
1 East gadgets -423 -354 94 2036 neutral high
2 North gadgets 723 77 1037 1115 very h… very h…
3 South gadgets 2022 -945 -610 200 low low
4 West gadgets 1204 426 433 -136 low low
5 East widgets -728 -51 -801 -342 very l… very l…
6 North widgets 2129 -517 822 -897 high very h…
7 South widgets 1123 -1450 1004 672 high neutral
8 West widgets 633 790 783 -315 neutral neutral
# … with abbreviated variable names ¹expenses_2020, ²satisfaction_2019,
# ³satisfaction_2020