regex - Find regular expression string and replace it in R -
the purpose tidy dataset cleaning variable names replications. typical example:
var.name <- "blue cat" #"true" variable name
is found in dataset following entries/replications:
char <- c("blue cat","blue cat blueing","blue cats", "blue cat dog", "blue catts cat","blue cat cat")
the ideal result of above replications renamed var.name, ie "blue cat". following grep(..) that:
char[grep(paste("blue cat", collapse="|"), char, value=f)] <- var.name
the drawback of method 1 has manually search & enter char occurrences. ideal solution parse "blue cat" regular expression , replace string matched.
any ideas? much.
if need parse regular expression, suffice.
sub('.*blue cat.*', var.name, char) # [1] "blue cat" "blue cat" "blue cat" "blue cat" "blue cat" "blue cat"
Comments
Post a Comment