我有一个dataframe (dataframe 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))然后我有另一个从上面的dataframe A中提取的dataframe (dataframe 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))我想做的是从dataframe A中的文本中减去dataframe B中的文本,得到dataframe 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."有人能帮我吗?
发布于 2020-10-28 03:34:20
Base R不提供在pattern上矢量化的字符串替换。你可以这样做:
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."https://stackoverflow.com/questions/64561548
复制相似问题