标签: stringr

如何在R中将字符列拆分为多列

我有一个数据框x

dput(x)
structure(list(District = structure(c(6L, 6L, 6L, 6L, 6L, 6L), .Label = c("District - Central (06)", 
"District - East (04)", "District - New Delhi (05)", "District - North (02)", 
"District - North East (03)", "District - North West (01)", "District - South (09)", 
"District - South West (08)", "District - West (07)"), class = "factor"), 
    Age = structure(c(103L, 1L, 2L, 14L, 25L, 36L), .Label = c("0", 
    "1", "10", "100+", "11", "12", "13", "14", "15", "16", "17", 
    "18", "19", …
Run Code Online (Sandbox Code Playgroud)

split r stringr

2
推荐指数
1
解决办法
6434
查看次数

从R中的字符串中匹配提取国家名称

我一直在从网站上抓取评论数据,在此过程中,我能够获取包含用户名、评论数量、评论日期和国家/地区信息的字符串向量。它们看起来大致是这样的

raw <- c("Anna (1025) - North Carolina, USA - DEC 20, 2017", 
"James (10) - - MEXICO - NOV 22, 2017", 
"Susane (222) - Oulu, FINLAND - JUNE 1, 2016", 
"Alex (20000) - SOUTH KOREA- MAR 11, 2015")
Run Code Online (Sandbox Code Playgroud)

到目前为止,我可以提取名称、评论编号和日期,因为它们位于定义的位置或具有一致的格式。问题在于,国家/地区名称格式的位置不一致,并且每个字符串中的各个数据点没有一致地用逗号或破折号分隔。仅提取大写字符串就会遇到缺少国家或名称中有两个部分的国家的问题。

地图包包含国家/地区列表。有没有一种方法可以用来str_extract_allstringr国家/地区列表向量中查找匹配项并提取该匹配项?

r data-processing web-scraping stringr dplyr

2
推荐指数
1
解决办法
4344
查看次数

case_when 与部分字符串匹配和 contains()

我正在使用一个数据集,其中有许多名为 status1、status2 等的列。在这些列中,它表示某人是否豁免、完整、注册等。

不幸的是,豁免投入并不一致;这是一个示例:

library(dplyr)

problem <- tibble(person = c("Corey", "Sibley", "Justin", "Ruth"),
                  status1 = c("7EXEMPT", "Completed", "Completed", "Pending"),
                  status2 = c("exempt", "Completed", "Completed", "Pending"),
                  status3 = c("EXEMPTED", "Completed", "Completed", "ExempT - 14"))
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用 case_when() 来创建一个具有最终状态的新列。如果它说已完成,那么它们就已完成。如果它说豁免但没有说完整,那么他们就豁免了。

重要的部分是我希望我的代码使用 contains("status") 或一些仅针对状态列且不需要全部键入的等效项,并且我希望它只需要部分字符串匹配即可豁免。

至于将 contains 与 case_when 一起使用,我看到了这个示例,但我无法将其应用到我的案例中:mutate with case_when and contains

这是我到目前为止尝试使用的,但正如你可以猜到的,它没有起作用:

library(purrr)
library(dplyr)
library(stringr)
solution <- problem %>%
  mutate(final= case_when(pmap_chr(select(., contains("status")), ~
    any(c(...) == str_detect(., "Exempt") ~ "Exclude",
               TRUE ~ "Complete"
  ))))
Run Code Online (Sandbox Code Playgroud)

这是我想要的最终产品的样子:

solution <- tibble(person = c("Corey", "Sibley", "Justin", "Ruth"), …
Run Code Online (Sandbox Code Playgroud)

string r contains stringr dplyr

2
推荐指数
1
解决办法
4292
查看次数

将 stringr::str_glue 与管道一起使用

我想str_glue与管道一起使用。我的代码:

library(tidyverse)

x <- c('john', 'bell', 'mary', 'cott')

x %>% 
  str_to_title(.) %>% 
  str_glue('Hi, {.}. How are you?')
Run Code Online (Sandbox Code Playgroud)

但是,出现以下错误消息:

错误:所有未命名参数的长度必须为 1

期望输出:

Hi, John. How are you?
Hi, Bell. How are you?
Hi, Mary. How are you?
Hi, Cott. How are you?
Run Code Online (Sandbox Code Playgroud)

r stringr

2
推荐指数
2
解决办法
2663
查看次数

将 str_detect 映射到字符串列表以检测第二个字符串列表

获取字符串列表:

strings <- c("ABC_XZY", "qwe_xyz", "XYZ")
Run Code Online (Sandbox Code Playgroud)

我想获取其中strings不包含特定子字符串的所有元素

avoid <- c("ABC")
Run Code Online (Sandbox Code Playgroud)

我可以做这个

library(stringr)
library(dplyr)
library(purrr)

strings %>% 
   .[!map_lgl(., str_detect, avoid)]
[1] "qwe_xyz" "XYZ"
Run Code Online (Sandbox Code Playgroud)

我想做的是指定几个子字符串

avoid_2 <- c("ABC", "qwe")
Run Code Online (Sandbox Code Playgroud)

然后像以前一样映射列表(不起作用)

strings %>% 
   .[!map_lgl(., str_detect, avoid_2)]
Error: Result 1 must be a single logical, not a logical vector of length 2
Run Code Online (Sandbox Code Playgroud)

我想要的是

[1] "XYZ"
Run Code Online (Sandbox Code Playgroud)

错误很明显 - 的每个元素都string为 的每个元素生成一个逻辑avoid_2,总共 2 个逻辑/元素,并且map_lgl只能处理一个/元素。

我当然可以单独处理每个子字符串,但我不想 - 我想制作一个子字符串列表

不想要,但确实有效

strings %>%
  .[!map_lgl(., str_detect, "ABC")] %>% 
  .[!map_lgl(., str_detect, "qwe")]
Run Code Online (Sandbox Code Playgroud)

r stringr purrr

2
推荐指数
1
解决办法
3773
查看次数

加入基于数据框的字符串模糊匹配

library(tidyverse)
library(fuzzyjoin)
df1 <- tibble(col1 = c("Apple Shipping", "Banana Shipping", "FedEX USA Ground",
                       "FedEx USA Commercial", "FedEx International"),
              col2 = 1:5)
#> # A tibble: 5 x 2
#>   col1                  col2
#>   <chr>                <int>
#> 1 Apple Shipping           1
#> 2 Banana Shipping          2
#> 3 FedEX USA Ground         3
#> 4 FedEx USA Commercial     4
#> 5 FedEx International      5

df2 <- tibble(col3 = c("Banana", "FedEX USA"), col4 = c(700, 900))
#> # A tibble: 2 x 2
#> …
Run Code Online (Sandbox Code Playgroud)

join r stringr dplyr fuzzyjoin

2
推荐指数
1
解决办法
580
查看次数

在 R 中使用 str_detect() 检测整个单词

我在 R 中有一个字符串:

c("FLT1", "FLT1P1", "FLT1-FLT2", "SGY-FLT1, GPD")
Run Code Online (Sandbox Code Playgroud)

我想保留所有具有 FLT1 的匹配项,但在添加其他字母数字字符时则不保留。换句话说,我想保留除第二个条目之外的所有条目,因为它们都提到了 FLT1,但第二个条目提到了 FLT1P1。

当我使用 str_detect 时,它返回所有内容为 true:

str_detect(string, "FLT1")
[1] TRUE TRUE TRUE TRUE
Run Code Online (Sandbox Code Playgroud)

任何人都可以建议仅退回提及 FLT1 的物品的最佳方法吗?

r stringr

2
推荐指数
1
解决办法
8452
查看次数

stringr::str_replace_all 的基本 R 替代方案,带有 c(pattern1 = replacement1) 选项

这个问题stringr::str_replace_all可能在有这个选项之前(或者众所周知)寻求类似的答案。我正在复制下面我的答案的要点,使用str_replace_all.


tr <- c("whatevs_1", "something_52", "whatevs_1something_52")

tr
#> [1] "whatevs_1"             "something_52"          "whatevs_1something_52"

patterns <- sprintf('_%s$', c('1','14','22','50','52','57','76','1018','2001','3301','6005'))
replacements <- sprintf('_%s' , c('R','I', 'P', 'O', 'C', 'D', 'M', 'L',   'S',   'K',   'G'))
                        
names(replacements) <- patterns

stringr::str_replace_all(tr, replacements)
#> [1] "whatevs_R"            "something_C"          "whatevs_1something_C"
Run Code Online (Sandbox Code Playgroud)

您将如何在 R 基础上实现上述目标?

提供的最佳选择是 for 循环。只是想知道是否有人同时想到了更好的选择。

regex r stringr

2
推荐指数
1
解决办法
499
查看次数

如何从字母和字母自动构造“A”=“a”、“B”=“b”等形式的表达式?

我最近读的解决方案,以在R4DS锻炼。它包括以下代码:

library(stringr)
replacements <- c("A" = "a", "B" = "b", "C" = "c", "D" = "d", "E" = "e",
                  "F" = "f", "G" = "g", "H" = "h", "I" = "i", "J" = "j", 
                  "K" = "k", "L" = "l", "M" = "m", "N" = "n", "O" = "o", 
                  "P" = "p", "Q" = "q", "R" = "r", "S" = "s", "T" = "t", 
                  "U" = "u", "V" = "v", "W" = "w", "X" = "x", …
Run Code Online (Sandbox Code Playgroud)

expression r stringr

2
推荐指数
1
解决办法
63
查看次数

在两个不同的向量上匹配相同的字符串

假设我们有两个不同的数据集:

数据集 A:

ids        name          price
1234       bread         1.5
245r7      butter        1.2
123984     red wine      5
43498      beer          1
235897     cream         1.8
Run Code Online (Sandbox Code Playgroud)

数据集 B:

ids          name       price
24908        lait       1
1234,089     pain       1.7
77289,43498  bière      1.5
245r7        beurre     1.4
Run Code Online (Sandbox Code Playgroud)

我的目标是匹配所有共享至少一个 ID 的产品,并将它们组合成一个新的数据集,如下所示:

id       a_name      b_name     a_price      b_price
1234     bread       pain       1.5          1.7
245r7    butter      beurre     1.2          1.4
43498    beer        bière      1            1.5
Run Code Online (Sandbox Code Playgroud)

这是否可行使用stringr或任何其他 R 包?

r stringr dplyr

2
推荐指数
1
解决办法
42
查看次数