Bắt đầu ngayBắt đầu miễn phí

Changing case to ease matching

A simple solution to working with strings in mixed case, is to simply transform them into all lower or all upper case. Depending on your choice, you can then specify your pattern in the same case.

For example, while looking for "cat" finds no matches in the following string,

x <- c("Cat", "CAT", "cAt") 
str_view(x, "cat")

transforming the string to lower case first ensures all variations match.

str_view(str_to_lower(x), "cat")

See if you can find the catcidents that also involved dogs. You'll see a new rebus function called whole_word(). The argument to whole_word() will only match if it occurs as a word on its own, for example whole_word("cat") will match cat in "The cat " and "cat." but not in "caterpillar".

A character vector of cat-related accidents has been pre-defined in your workspace as catcidents.

Bài tập này là một phần của khóa học

String Manipulation with stringr in R

Xem khóa học

Bài tập tương tác thực hành trực tiếp

Hãy thử làm bài tập này bằng cách hoàn thành đoạn mã mẫu này.

# catcidents has been pre-defined
head(catcidents)

# Construct pattern of DOG in boundaries
whole_dog_pattern <- ___

# See matches to word DOG
___
Chỉnh sửa và Chạy Mã