R/dplyr filter() 可以使用 grepl 函数,但不能使用 '=='

R/dplyr filter() can work using grepl function, but can't work using '=='

有数据框test_data,有两个过滤代码,第二个不行。我认为它们是一样的,谁能帮忙解释一下?谢谢!

# OK 
test_data %>% filter(grepl("Quantit",title))

# Can't work
test_data %>% filter(title=="Quantit\xa8\xa4")

您可以通过 'escaping' 为字符串添加引号,并在其前面加上 \:

test_data <- data.frame(title="\"Quantit\xa8\xa4\"")


library(dplyr)


# Working `grepl` version to test
test_data %>% filter(grepl("Quantit",title))
#>               title
#> 1 "Quantit\xa8\xa4"

# Using `==` and matching string
test_data %>% filter(title=="\"Quantit\xa8\xa4\"")
#>               title
#> 1 "Quantit\xa8\xa4"

reprex package (v2.0.1)

于 2022-05-09 创建