Load needed library
First of load the tidyverse
package, because the following code uses some helpful functions from this package. Maybe packages like rmarkdown
or knitr
need to be loaded as well.
library(tidyverse)
1. Filter rows by rownames in the index column
mtcars
is a variable of type data frame and one of the built-in data sets in R. The Motor Trend Car Road Testsdata set contains 11 aspects of cars collected by a magazine in 1971 (see table at the end of this post).
TL;DR summary
The rows of the data frame are filtered by using filter
and grepl
to find all matches in the named index column by comparing it to the regular expression Honda|Toyota
.
filtered_cars <- mtcars %>%
filter(!grepl("Honda|Toyota", rownames(mtcars)))
In-Depth Explanation:
The car data is piped into the function dplyr::filter()
to extract a subset of only the rows that fullfill all the given conditions. This condition is given with a data-mask expression as a function argument. For this expression we use grep
and the regular expression Honda|Toyota
. Since the names of the cars are not in a regular data column they cannot be accessed with something like mtcars$gear
. Therefore rownames(mtcars)
must be used get a vector of the names in the index column. The result of this piped expression is assigned to the filtered_cars
variable.
Just by looking at the count of the resulting data frame it is noticeable that a few rows have been removed:
mtcars %>% count() # 32
filtered_cars %>% count() # 29
2. Print data frame as markdown formatted table
knitr::kable(
filtered_cars[1:5,],
caption = "A table in a markdown document (subset of mtcar data set)"
)
Output:
| | mpg| cyl| disp| hp| drat| wt| qsec| vs| am| gear| carb|
|:-----------------|----:|---:|----:|---:|----:|-----:|-----:|--:|--:|----:|----:|
|Mazda RX4 | 21.0| 6| 160| 110| 3.90| 2.620| 16.46| 0| 1| 4| 4|
|Mazda RX4 Wag | 21.0| 6| 160| 110| 3.90| 2.875| 17.02| 0| 1| 4| 4|
|Datsun 710 | 22.8| 4| 108| 93| 3.85| 2.320| 18.61| 1| 1| 4| 1|
|Hornet 4 Drive | 21.4| 6| 258| 110| 3.08| 3.215| 19.44| 1| 0| 3| 1|
|Hornet Sportabout | 18.7| 8| 360| 175| 3.15| 3.440| 17.02| 0| 0| 3| 2|
...
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…