简体   繁体   English

根据另一列中的值从 R 中的字符列中删除字符

[英]Remove characters from a character column in R based on values in another column

I have a data frame of team names and abbreviations.我有一个团队名称和缩写的数据框。 Currently, the team name column also contains the abbreviation.目前,团队名称栏也包含缩写。 I'm trying to remove the abbreviation from the team name column to avoid repeating information.我试图从团队名称列中删除缩写以避免重复信息。

Here's my current data frame:这是我当前的数据框:

Team Abbr.团队缩写。 Team Name队名
ARK方舟 ArkansasARK阿肯色方舟
BSU北体大 Boise StateBSU博伊西州立大学
DART DART DartmouthDART达特茅斯DART

My desired output is this:我想要的 output 是这样的:

Team Abbr.团队缩写。 Team Name队名
ARK方舟 Arkansas阿肯色州
BSU北体大 Boise State博伊西 State
DART DART Dartmouth达特茅斯

Thanks!谢谢!

We may use str_remove which is vectorized for both pattern and string我们可以使用str_remove ,它对模式和字符串都进行了矢量化

library(dplyr)
library(stringr)
df1 %>% 
  mutate(TeamName = str_remove(TeamName, fixed(TeamAbbr)))
  TeamAbbr    TeamName
1      ARK    Arkansas
2      BSU Boise State
3     DART   Dartmouth

If we want to do this from a column in different dataset and the lengths are different, one option is to paste ( str_c ) the elements together with |如果我们想从不同数据集中的列执行此操作并且长度不同,一种选择是将元素与| pastestr_c )在一起( OR ) as pattern ( OR ) 作为模式

df2 %>% 
  mutate(TeamName = str_remove(TeamName, str_c(TeamAbbr,
       collapse = "|"))) 

data数据

df1 <- structure(list(TeamAbbr = c("ARK", "BSU", "DART"), 
TeamName = c("ArkansasARK", 
"Boise StateBSU", "DartmouthDART")), class = "data.frame", row.names = c(NA, 
-3L))

Here is a base R solution.这是一个基本的 R 解决方案。 Loop by rows with apply and replace the 1st column's value in the 2nd with the emp string.使用apply按行循环,并将第二列中第一列的值替换为 emp 字符串。

df1 <- read.table(text = "
'Team Abbr.'    'Team Name'
ARK     ArkansasARK
BSU     'Boise StateBSU'
DART    DartmouthDART
", header = TRUE)
df1
#>   Team.Abbr.      Team.Name
#> 1        ARK    ArkansasARK
#> 2        BSU Boise StateBSU
#> 3       DART  DartmouthDART

df1$Team.Name <- apply(df1, 1, \(x) sub(x[1], "", x[2]))
df1
#>   Team.Abbr.   Team.Name
#> 1        ARK    Arkansas
#> 2        BSU Boise State
#> 3       DART   Dartmouth

Created on 2022-02-16 by the reprex package (v2.0.1)reprex package (v2.0.1) 创建于 2022-02-16

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM