Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
897 views
in Technique[技术] by (71.8m points)

filter - R: Filtering rows by “contains” text within row names

I am working with the mtcars dataset in R. My assignment is to print the rows corresponding to Honda and Toyota cards using knitr::kable(). I was directed to this for help: https://r4ds.had.co.nz/r-markdown.html#table

However that section only talks about printing the first 5 rows of the dataset, not about filtering it at all. I clicked to read more about the function but it was all foreign to me.

The best I can tell is that the make and model of the cars are the row names. So I need to filter the results to print only rows whose names contain “Honda” or “Toyota” and I need to do it using knitr::kable().

I have tried creating subsets, but unsure how to do so using row names. Also wouldn’t know how to search whether the row names contain the text “Honda” or “Toyota”.

This is my first day working with R and my only coding experience before today was some C# two+ years ago. This is just very frustrating to me because I could do this in Excel in less than 30 seconds. But R is like a foreign language and I don’t feel like the section of my textbook I was referred to explained the problem - especially for a brand new coder (and this class isn’t supposed to require any experience) . Appreciative of any help I can get!

question from:https://stackoverflow.com/questions/65864235/r-filtering-rows-by-contains-text-within-row-names

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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|
...

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...