2 Manipulating Rows
Select rows | filter() , distinct() , slice() family |
Arrange rows | arrange() |
Add rows | add_row() , bind_rows() |
Please copy the following code and paste them into a script in the RStudio. We will walk through them with the visual explanations from the dplyr cheat sheet.
Input
# make sure you loaded the tidyverse library
library(tidyverse)
# Get to know the data
?mtcars
mtcars
# Sample code from the dplyr cheat sheet
## 2.1 Select cases
filter(mtcars, mpg > 20)
distinct(mtcars, gear)
slice(mtcars, 10:15)
slice_sample(mtcars, n = 5, replace = TRUE)
slice_min(mtcars, mpg, prop = 0.25)
slice_max(mtcars, mpg, prop = 0.25)
slice_head(mtcars, n = 5)
slice_tail(mtcars, n = 5)
## 2.2 Arrange cases
arrange(mtcars, mpg)
arrange(mtcars, desc(mpg))
## 2.3 Add cases
add_row(cars, speed = 1, dist = 1)
### A showcase for bind_rows
x <- data.frame(
A = c('a', 'b'),
B = c('t', 'u'),
C = c(1, 2))
x
y <- data.frame(
A = c('c', 'v'),
B = c('d', 'w'),
D = c(TRUE, FALSE))
y
bind_rows(x, y)
2.1 Select Cases
Figure Source: dplyr Cheat Sheet, CC BY SA Posit Software, PBC
2.2 Arrange Cases
Figure Source: dplyr Cheat Sheet, CC BY SA Posit Software, PBC
2.3 Add Cases
Figure Source: dplyr Cheat Sheet, CC BY SA Posit Software, PBC
Practice 2
iris
is a data frame with 150 cases (rows) and 5 variables (columns) such as Petal.Width
and Species
. In the iris
data set, the cases with the minimum and maximum petal width belong to what species?
Click here for solutions
# solution 1
arrange(iris, Petal.Width)
# solution 2
slice_min(iris, Petal.Width, prop = 0.01)
slice_max(iris, Petal.Width, prop = 0.01)
# The case with the minimum petal width belongs to setosa.
# The case with the maximum petal width belongs to virginica.
This page is meant to introduce functions that help manipulate rows.
A pause here for questions.