本文介绍了字符串上的分隔行保留原始内容(&A)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在进行一个项目,其中我想要特定字符串的所有排列。我使用tidyr::separate_rows
对特定字符串进行拆分和复制,但我希望保留原始行。
require(dplyr)
require(tidyr)
temp <- tibble(raw_name = c("happy bank dba american bank and trust", " sohappy bank dba american bank"), clean_name = c("american bank and trust", "american bank"))
所以我现在做的是:
final <- temp %>%
separate_rows(raw_name, sep = "dba")
这会丢失我原来的行。我已经查看了文档,但我找不到.keep_all = TRUE
的版本。以下是上述separate_rows
的结果:
raw_name clean_name
<chr> <chr>
1 happy bank dba american bank and trust american bank and trust
2 " sohappy bank dba american bank" american bank
我当前的解决方案是创建一个带有违规观察的新DF,执行separate_rows
并重新绑定原始行。以下是我想要的结果:
raw_name clean_name
<chr> <chr>
1 happy bank dba american bank and trust american bank and trust
2 " sohappy bank dba american bank" american bank
3 "happy bank " american bank and trust
4 " american bank and trust" american bank and trust
5 " sohappy bank " american bank
6 " american bank" american bank
谢谢大家!
推荐答案
分隔行后,可以绑定原始数据集
library(dplyr)
library(tidyr)
temp %>%
separate_rows(raw_name, sep="\s*dba\s*") %>%
bind_rows(temp, .)
# A tibble: 6 x 2
# raw_name clean_name
#* <chr> <chr>
#1 "happy bank dba american bank and trust" american bank and trust
#2 " sohappy bank dba american bank" american bank
#3 "happy bank" american bank and trust
#4 "american bank and trust" american bank and trust
#5 " sohappy bank" american bank
#6 "american bank" american bank
这篇关于字符串上的分隔行保留原始内容(&A)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!