R - the number of lines in a line with the @ symbol

I have a list of records with IDs (some of which are usernames and some of which are email addresses). I would like to know how many email addresses. I thought an easy way to do this was to count how many lines the @ symbol contains, but I can't seem to get the function to work for that. Any help is appreciated!

Example dataset:

x <- c("1234@aol.com", "johnnyApple", "tomb@gmail.com")

      

+3


source to share


3 answers


Both answers are completely correct so far, but if you are looking for an email address, the less likely false positives are:



x <- c("1234@aol.com", "johnnyApple", "tomb@gmail.com")  
sum(regexpr(".*@.*\\..*",x) != -1)

      

+6


source


Try:



x <- c("1234@aol.com", "johnnyApple", "tomb@gmail.com")
sum(grepl("@", x))

      

+2


source


Assuming the data df

, you can try

length(grep(pattern="@", df$V1))
[1] 2

      

+1


source







All Articles