首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >假设"Text B“是"Text A”的子集,如何从"Text A“中减去"Text B”?

假设"Text B“是"Text A”的子集,如何从"Text A“中减去"Text B”?
EN

Stack Overflow用户
提问于 2020-10-28 03:24:25
回答 1查看 31关注 0票数 0

我有一个dataframe (dataframe A),其中每一行都包含一些文本:

代码语言:javascript
复制
df_A = structure(list(Text = c("To this end, we strongly support the Commission in its determination to promote the renewed Lisbon Strategy and we encourage national governments to show their determination in this regard by implementing structural reforms in Europe. In fact, incoming information since the last Governing Council meeting indicates a more protracted weakness of the euro area economy, the persistence of prominent downside risks and muted inflationary pressures. This is the type of text that I would like to cancel and keep only the other one. This is reflected in the new staff projections, which show a further downgrade of the inflation outlook.", 
"To this end, we strongly support the Commission in its determination to promote the renewed Lisbon Strategy and we encourage national governments to show their determination in this regard by implementing structural reforms in Europe. There are super skilled people in SO who will hopefully help me sort this out. In fact, incoming information since the last Governing Council meeting indicates a more protracted weakness of the euro area economy, the persistence of prominent downside risks and muted inflationary pressures. This is reflected in the new staff projections, which show a further downgrade of the inflation outlook.", 
"To this end, we strongly support the Commission in its determination to promote the renewed Lisbon Strategy and we encourage national governments to show their determination in this regard by implementing structural reforms in Europe. I would really like to remove this bit and I hope that SE will sort it out. In fact, incoming information since the last Governing Council meeting indicates a more protracted weakness of the euro area economy, the persistence of prominent downside risks and muted inflationary pressures. This is reflected in the new staff projections, which show a further downgrade of the inflation outlook."
)), class = "data.frame", row.names = c(NA, -3L))

然后我有另一个从上面的dataframe A中提取的dataframe (dataframe B):

代码语言:javascript
复制
df_B = structure(list(Text = c("This is the type of text that I would like to cancel and keep only the other one.", 
"There are super skilled people in SO who will hopefully help me sort this out.", 
"I would really like to remove this bit and I hope that SE will sort it out."
)), class = "data.frame", row.names = c(NA, -3L))

我想做的是从dataframe A中的文本中减去dataframe B中的文本,得到dataframe C:

代码语言:javascript
复制
# df_C = df_A - df_B

[[1]]"To this end, we strongly support the Commission in its determination to promote the renewed Lisbon Strategy and we encourage national governments to show their determination in this regard by implementing structural reforms in Europe. In fact, incoming information since the last Governing Council meeting indicates a more protracted weakness of the euro area economy, the persistence of prominent downside risks and muted inflationary pressures. This is reflected in the new staff projections, which show a further downgrade of the inflation outlook."

[[2]] "To this end, we strongly support the Commission in its determination to promote the renewed Lisbon Strategy and we encourage national governments to show their determination in this regard by implementing structural reforms in Europe. In fact, incoming information since the last Governing Council meeting indicates a more protracted weakness of the euro area economy, the persistence of prominent downside risks and muted inflationary pressures. This is reflected in the new staff projections, which show a further downgrade of the inflation outlook."
 
[[3]] "To this end, we strongly support the Commission in its determination to promote the renewed Lisbon Strategy and we encourage national governments to show their determination in this regard by implementing structural reforms in Europe. In fact, incoming information since the last Governing Council meeting indicates a more protracted weakness of the euro area economy, the persistence of prominent downside risks and muted inflationary pressures. This is reflected in the new staff projections, which show a further downgrade of the inflation outlook."

有人能帮我吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-28 03:34:20

Base R不提供在pattern上矢量化的字符串替换。你可以这样做:

代码语言:javascript
复制
mapply(
  function(x,y) gsub(y, "", x, fixed = TRUE),
  x = df_A$Text,
  y = df_B$Text,
  USE.NAMES = FALSE
)

[1] "To this end, we strongly support the Commission in its determination to promote the renewed Lisbon Strategy and we encourage national governments to show their determination in this regard by implementing structural reforms in Europe. In fact, incoming information since the last Governing Council meeting indicates a more protracted weakness of the euro area economy, the persistence of prominent downside risks and muted inflationary pressures.  This is reflected in the new staff projections, which show a further downgrade of the inflation outlook."
[2] "To this end, we strongly support the Commission in its determination to promote the renewed Lisbon Strategy and we encourage national governments to show their determination in this regard by implementing structural reforms in Europe.  In fact, incoming information since the last Governing Council meeting indicates a more protracted weakness of the euro area economy, the persistence of prominent downside risks and muted inflationary pressures. This is reflected in the new staff projections, which show a further downgrade of the inflation outlook."
[3] "To this end, we strongly support the Commission in its determination to promote the renewed Lisbon Strategy and we encourage national governments to show their determination in this regard by implementing structural reforms in Europe.  In fact, incoming information since the last Governing Council meeting indicates a more protracted weakness of the euro area economy, the persistence of prominent downside risks and muted inflationary pressures. This is reflected in the new staff projections, which show a further downgrade of the inflation outlook."

stringr包基本上是stringi包的替代接口,它提供了快速的矢量化字符串替换和一些方便的实用函数:

代码语言:javascript
复制
stringr::str_remove(df_A$Text, df_B$Text)
[1] "To this end, we strongly support the Commission in its determination to promote the renewed Lisbon Strategy and we encourage national governments to show their determination in this regard by implementing structural reforms in Europe. In fact, incoming information since the last Governing Council meeting indicates a more protracted weakness of the euro area economy, the persistence of prominent downside risks and muted inflationary pressures.  This is reflected in the new staff projections, which show a further downgrade of the inflation outlook."
[2] "To this end, we strongly support the Commission in its determination to promote the renewed Lisbon Strategy and we encourage national governments to show their determination in this regard by implementing structural reforms in Europe.  In fact, incoming information since the last Governing Council meeting indicates a more protracted weakness of the euro area economy, the persistence of prominent downside risks and muted inflationary pressures. This is reflected in the new staff projections, which show a further downgrade of the inflation outlook."
[3] "To this end, we strongly support the Commission in its determination to promote the renewed Lisbon Strategy and we encourage national governments to show their determination in this regard by implementing structural reforms in Europe.  In fact, incoming information since the last Governing Council meeting indicates a more protracted weakness of the euro area economy, the persistence of prominent downside risks and muted inflationary pressures. This is reflected in the new staff projections, which show a further downgrade of the inflation outlook."
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64561548

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档