小编Jan*_*tz 的帖子

从新列中的多列中提取字符串

我想在不同的列中找到一个单词并在新的列中对其进行变异。

“数据”是一个例子,“目标”是我想要的。我尝试了很多,但没有得到工作。

 library(dplyr)
 library(stringr)

 data <- tibble(
    component1 = c(NA, NA, "Word", NA, NA, "Word"),
    component2 = c(NA, "Word", "different_word", NA, NA, "not_this")
    )

 goal <- tibble(
    component1 = c(NA, NA, "Word", NA, NA, "Word"),
    component2 = c(NA, "Word", "different_word", NA, NA, "not_this"),
    component = c(NA, "Word", "Word", NA, NA, "Word")
    )


not_working <- data %>%
     mutate(component = across(starts_with("component"), ~ str_extract(.x, "Word")))
Run Code Online (Sandbox Code Playgroud)

r stringr dplyr

4
推荐指数
1
解决办法
376
查看次数

标签 统计

dplyr ×1

r ×1

stringr ×1