I am working with a dataset where the data my variables column is in the format blah blah [text I actually want]. I have wrote a line to try to replace every data point in the variables column with a new data point with just text I actually want.
I thought I finally cracked it, but it does not seem to actually be working so far.
melted$variable = str_sub(
melted$variable, start = gregexpr(
pattern ="\\[",melted$variable)[1], end = (
str_length(melted$variable) - 1
)
)
melted is my data set, and variable is the column name
We can use sub and extract everything between [ and ]
sub(".*\\[(.*)\\].*", "\\1", x)
#[1] "ex1" "ex2"
Or using str_extract
stringr::str_extract(x, "(?<=\\[).*(?=\\])")
#[1] "ex1" "ex2"
where x is
x <- c("blah blah [ex1]", "blah blah [ex2]")
which can be replaced with melted$variable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With