Reputation: 755
I'm trying to clean up the following dataset to maintain some consistency within the Changes field.
Input:
test_data <- data.frame(ID=c('[email protected]', '[email protected]'),
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)
I want to delete all lines within a given field where the first ">" is followed by "Everything. I'll like to remove that entire line.
For cases where "Everything" occurs after the second ">", i'll like to replace from "Everything" to ":" with "q"
Output:
out_data <- data.frame(ID=c('[email protected]', '[email protected]'),
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)
Thanks.
Upvotes: 0
Views: 62
Reputation: 11597
Maybe not the best solution, but it gets what you want in the 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 [email protected]
2 [email protected]
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
Upvotes: 1