2

Let's say if the data frame is df with column foo. Is there a way to remove a string starting with @ in data frame column in R.

Example:

df <- data.frame(foo=c("@john is awesome than @steve", 
                                        "@steve is good","@mike is nice"))

df
                           foo
1 @john is awesome than @steve
2               @steve is good
3                @mike is nice

How do I remove the entire name @john,@Steve, @mike all that starts with @.

The Final output should be

               foo
1  is awesome than 
2          is good
3          is nice

I would like to get rid of all the strings from a column foo in data frame df that start with delimiter @.

Developer
  • 917
  • 2
  • 9
  • 25
Amit Ugle
  • 139
  • 1
  • 1
  • 10

2 Answers2

4

A combination of mine and Richard Scriven's comments.

df$foo <- gsub(" ?@\\w+ ?", "", df$foo)
df
#               foo
# 1 is awesome than
# 2         is good
# 3         is nice

The @\\w+ matches the ampersat followed by one or more letters. The ? matches an optional space at the beginning and the end.

So altogether it looks for a match of

[optional single space]@[one word][optional single space]
Rich Scriven
  • 97,041
  • 11
  • 181
  • 245
user20650
  • 24,654
  • 5
  • 56
  • 91
0

You may want to have a look at the package stringr and use str_replace_all to match the regular expression starting with @ and any following word as:

library("stringr")

str_replace_all(df$foo, " ?@\\w+ ?", "")

[1] "is awesome than steve" 
[2] "is good"             
[3] "is nice"
gented
  • 1,620
  • 1
  • 16
  • 20