r语言 - 如何用另一列的名称替换初始(第一个或最后一个)?



如何用全名替换首字母(如果首字母与另一列中的全名匹配)?

我有这样的数据:

data <- data.frame(name = c("Acorus americanus", "Nothocalais cuspidata", "Elymus repens", "Elymus hmmmm", "Acorus americanus"),
synonym = c("A. calamus", "Agoseris cuspidata", "Agropyron r.", "Elymus sp.", "S. americanus"))
name            synonym
1     Acorus americanus         A. calamus
2 Nothocalais cuspidata Agoseris cuspidata
3         Elymus repens       Agropyron r.
4          Elymus hmmmm         Elymus sp.
5     Acorus americanus      S. americanus

我如何用名称替换首字母,从而得到这个?

name            synonym
1     Acorus americanus     Acorus calamus
2 Nothocalais cuspidata Agoseris cuspidata
3         Elymus repens   Agropyron repens 
4          Elymus hmmmm         Elymus sp.
5     Acorus americanus      S. americanus

还有其他缩写,如sp.,var.ssp.,我不想更改为名称,但它们都不是单个字母。另外,如果在另一列中与名字的第一个字母不匹配,我希望保留首字母。

此解决方案使用stringr::str_extract()测试共享的首字母,然后使用stringr::str_replace()替换单字母缩写。(您也可以使用基础grep()gsub())。

library(stringr)
library(dplyr)
data %>%
mutate(
synonym = if_else(
str_extract(synonym, "^\w") == str_extract(name, "^\w"),
str_replace(
synonym,
"^\w\.", 
str_extract(name, "^\w+")
),
synonym
),
synonym = if_else(
str_extract(synonym, "(?<=\s)\w") == str_extract(name, "(?<=\s)\w"),
str_replace(
synonym,
"\w\.$", 
str_extract(name, "\w+$")
),
synonym
)
)
name            synonym
1     Acorus americanus     Acorus calamus
2 Nothocalais cuspidata Agoseris cuspidata
3         Elymus repens   Agropyron repens
4          Elymus hmmmm         Elymus sp.
5     Acorus americanus      S. americanus

另一个解决方案:

library(tidyverse)
data %>%
summarise(read.table(text=t(cur_data())))%>%
mutate(across(everything(), ~if_else(str_detect(lag(., def=""), .), lag(.), .)))%>%
unite(result, sep = ' ')%>%
mutate(rep(names(data), nrow(data)))%>%
unstack()

name            synonym
1     Acorus americanus     Acorus calamus
2 Nothocalais cuspidata Agoseris cuspidata
3         Elymus repens   Agropyron repens
4          Elymus hmmmm         Elymus sp.
5     Acorus americanus      S. americanus

相关内容

  • 没有找到相关文章

最新更新