【问题标题】:How to subtract "Text B" from "Text A" given that "Text B" is a subset of "Text A"?鉴于“文本 B”是“文本 A”的子集,如何从“文本 A”中减去“文本 B”?
【发布时间】:2021-02-10 03:38:23
【问题描述】:

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

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))

然后我从上面的数据帧 A 中提取了另一个数据帧(数据帧 B):

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))

我想做的是从数据框A中的文本中减去数据框B中的文本以获得数据框C:

# 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."

有人可以帮我做吗?

【问题讨论】:

    标签: r string dataframe replace


    【解决方案1】:

    Base R 不提供基于模式向量化的字符串替换。您可以执行以下操作:

    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 包的替代接口,提供快速矢量化字符串替换和一些方便的实用功能。:

    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."
    

    【讨论】:

      猜你喜欢
      • 2021-04-10
      • 1970-01-01
      • 1970-01-01
      • 2013-04-27
      • 1970-01-01
      • 1970-01-01
      • 2018-07-15
      • 2016-02-17
      • 2011-03-22
      相关资源
      最近更新 更多