我正在尝试清理以下数据集,以便在“更改”字段中保持一定的一致性。
输入:
test_data <- data.frame(ID=c('john@xxx.com', 'sally@xxx.com'),
Changes=c('3 max cost changes
productxyz > pb100 > a : Max cost decreased from $0.98 to $0.83
productxyz > pb2 > a : Max cost decreased from $1.07 to $0.91
productxyz > pb2 > b : Max cost decreased from $0.65 to $0.55',
'2 max cost changes
productabc > Everything else in "auto & truck maintenance" : Max CPC increased from $0.81 to $0.97
productabc > pb1000 > x : Max cost decreased from $1.44 to $1.22
productabc > pb10000 > Everything else in pb10000 : Max CPC increased from $0.63 to $0.76'), stringsAsFactors=FALSE)
我想删除给定字段中第一个&#34;&gt;&#34;的所有行。接下来是&#34; Everything。我想删除整行。
对于&#34; Everything&#34;在第二个&#34;&gt;&#34;之后发生,我希望从&#34; Everything&#34;到&#34;:&#34;用&#34; q&#34;
输出:
out_data <- data.frame(ID=c('john@xxx.com', 'sally@xxx.com'),
Changes=c('3 max cost changes
productxyz > pb100 > a : Max cost decreased from $0.98 to $0.83
productxyz > pb2 > a : Max cost decreased from $1.07 to $0.91
productxyz > pb2 > b : Max cost decreased from $0.65 to $0.55',
'2 max cost changes
productabc > pb1000 > x : Max cost decreased from $1.44 to $1.22
productabc > pb10000 > q : Max CPC increased from $0.63 to $0.76'), stringsAsFactors=FALSE)
感谢。
答案 0 :(得分:1)
也许不是最好的解决方案,但它在test_data
:
clean_text <- function(x){
x <- gsub("(> .* > )Everything else in .* :", "\\1 q :", x)
x <- gsub("\n .* Everything else in .*?\n", "", x)
x
}
out_data <- test_data
out_data[,2] <- clean_text(test_data[,2])
out_data
ID
1 john@xxx.com
2 sally@xxx.com
Changes
1 3 max cost changes\n productxyz > pb100 > a : Max cost decreased from $0.98 to $0.83\n productxyz > pb2 > a : Max cost decreased from $1.07 to $0.91\n productxyz > pb2 > b : Max cost decreased from $0.65 to $0.55
2 2 max cost changes productabc > pb1000 > x : Max cost decreased from $1.44 to $1.22\n productabc > pb10000 > q : Max CPC increased from $0.63 to $0.76