I am a new R user and I need some help.
I have a dataframe that look like this :
Type ID
pink-blue-blue-green-END 125
blue-pink-END 145
green-green-pink-END 489
green-pink-blue-END 478
pink-green-green-blue-END 546
And I want all the rows where "blue" is before "pink", the results would be
Type ID
pink-blue-blue-green-END 125
green-pink-blue-END 478
pink-green-green-blue-END 546
And I would like to know another tips :) How I can do to delete all the "green" inside the list of character and having for example for the 1st row :
pink-blue-blue-END
Thanks for your help
We can use grep
to match the word "pink" followed by one or more characters followed by word "blue". Note that \\b
signify the word boundary
df1[grepl("\\bpink\\b-.*\\bblue\\b", df1$Type),]
# Type ID
#1 pink-blue-blue-green-END 125
#4 green-pink-blue-END 478
#5 pink-green-green-blue-END 546
For the second case, use gsub
df1$Type <- gsub("green-", "", df1$Type)