Find values starting with a specific word

Hello everyone,

My question might seem easy, but I have tried several things but still nothing works….!

I have a column, lets say called "a", that contains several thousands of "words".

What I want to do is to choose only those rows, where the value of "a" STARTS WITH "AC01".

I have tried the filter-function, but the problem is that it returns all rows CONTAINING "AC01", which sometimes is inside the word, but I only want the word when it starts with "AC01"
Anybody have any idea?

Best regards….

You need to use a regular expression that checks whether the substring of interest is at the start of the string. So, for example...

df <- data.frame(a = c("AC01Waq7", "AC2-wiggle", "AC01hithere"),
                 stringsAsFactors = FALSE)


1    AC01Waq7
2  AC2-wiggle
3 AC01hithere
filter(df, grepl("^AC01*", a))
1    AC01Waq7
2 AC01hithere

Or, if you want to stay in the tidyverse...


filter(df, str_detect(a, "^AC01*"))

Then another question, please.... what if I want to change all the values that start with "A01" to fx "BB"?

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

Thank you very much @ulfelder !
Although it seems my results become more correct if I dont type the Asterix (*) in the end :smiley:

My Best Regards!

I mean change the WHOLE string that starts with "A01" to "BB" . I am considering "ifelse"....but there might be better options...?

@Nice, do you mean change the whole string to "BB", or just replace the "A01" with "BB"?

@Nice, I don't see anything wrong with using ifelse. Something like...

mutate(df, a2 = ifelse(str_detect(a, "^AC01"), "BB", a)) 

Or, in base R...

df$a2 <- with(df, ifelse(grepl("^AC01", a), "BB", a))