在R中减去字符串

时间:2018-01-25 19:09:26

标签: r string tidyverse stringr

是否有一种简单的方法可以在tibble或data.frame中的列之间减去字符串?

例如,在下面的tibble中,有没有办法从a和c列轻松创建b列?类似于我如何从a和b创建c? (即c = a + b,所以b = c - a)。

// ...
B b;
A &a = b;
// ...

我希望该解决方案适用于a和b列中每个字符串中的任意数量的单词。

例如,我正在考虑下面代码中的某些内容(分成单词并进行成对比较),但它不太有效。

ex1 <- tibble(a = rep(c("orange", "green", "grey"), 2),
   b = rep(c("ball", "hockey puck"), each = 3),
   c = str_c(a, " ", b))

有什么想法吗?

2 个答案:

答案 0 :(得分:3)

其中任何一个都应该有效:

ex1 %>% 
  rowwise() %>% 
  mutate( b = sub(a, "", c) %>% str_trim() )

# # A tibble: 6 x 3
#        a            b                  c
#    <chr>        <chr>              <chr>
# 1 orange         ball        orange ball
# 2  green         ball         green ball
# 3   grey         ball          grey ball
# 4 orange  hockey puck orange hockey puck
# 5  green  hockey puck  green hockey puck
# 6   grey  hockey puck   grey hockey puck

ex1 %>% mutate( b = str_replace(ex1$c, ex1$a, "") %>% str_trim() )

# # A tibble: 6 x 3
#        a           b                  c
#    <chr>       <chr>              <chr>
# 1 orange        ball        orange ball
# 2  green        ball         green ball
# 3   grey        ball          grey ball
# 4 orange hockey puck orange hockey puck
# 5  green hockey puck  green hockey puck
# 6   grey hockey puck   grey hockey puck

答案 1 :(得分:0)

您可以编写执行此操作的函数

`%-%`=function(x,y)sub(paste0("\\s*",y,"\\s*",collapse="|"),"",x)
ex1$c%-%ex1$a # To obtain b ie c-a
[1] "ball"        "ball"        "ball"        "hockey puck" "hockey puck" "hockey puck"
ex1$c%-%ex1$b # To obtain a ie c-b
[1] "orange" "green"  "grey"   "orange" "green"  "grey"