"unused argument" error with grepl

When I'm using the function "grepl" for 1, 2, 3 or 4 keywords at a time, I don't have any issues:

test_03_13[,5] = grepl("distance", test_03_13$tweet, ignore.case = TRUE)
test_03_13[,5:6] = grepl("distance", "miles", test_03_13$tweet, ignore.case = TRUE)
test_03_13[,5:7] = grepl("distance", "miles", "kilometers", test_03_13$tweet, ignore.case = TRUE)
test_03_13[,5:8] = grepl("distance", "miles", "kilometers", "near", test_03_13$tweet, ignore.case = TRUE)

From the moment I put a 5th keyword, I have an "unused argument" error :

test_03_13[,5:9] = grepl("distance", "miles", "kilometers", "near", "radius", test_03_13$tweet, ignore.case = TRUE)
Error in grepl("distance", "miles", "kilometers", "near", "radius", test_03_13$tweet, :
unused argument (test_03_13$tweet)

Can someone explain to me what happened and what I can do to resolve this, please :smiley:

I am surprised it worked with the words separated by commas. The pattern argument should be a single string. To look for multiple possibilities, separate them with pipes.

DF <- data.frame(tweet = c("my kilometer", "The distance", "miles to go",
                            "measure the radius", "random text", "Now its near us"))
grepl("distance|miles|near|kilometer|radius", DF$tweet)
[1]  TRUE  TRUE  TRUE  TRUE FALSE  TRUE

The problem with pipes is that they mean "or"
grepl("distance" or "miles" or "kilometers", DF$tweet)
I'm looking for a function / argument combination that will classify the boolean variable by column, without having to copy and paste this code for my 500 keywords. You have an idea?

Are you looking for something like the following code?

DF <- data.frame(tweet = c("my kilometer", "The distance", "miles to go",
                            "measure the radius", "random text", "Now its near us"))
KEY <- c("distance","miles","near","kilometer","radius")
sapply(KEY, grepl, x = DF$tweet)
     distance miles  near kilometer radius
[1,]    FALSE FALSE FALSE      TRUE  FALSE
[2,]     TRUE FALSE FALSE     FALSE  FALSE
[3,]    FALSE  TRUE FALSE     FALSE  FALSE
[4,]    FALSE FALSE FALSE     FALSE   TRUE
[5,]    FALSE FALSE FALSE     FALSE  FALSE
[6,]    FALSE FALSE  TRUE     FALSE  FALSE

Yes exactly! Thank you!!! :smiley:
I just added test_03_13[,5:9] = before the function sapply so that my variables appear directly in my DF.

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.