1
votes

I have a simple Spark data frame strings and I cannot filter it by using the str_count function in the stringr package. For example:

> library(stringr)
> df = data.frame(long=c("AA", "BB"), short=c("A", "B"))
> df
  long short
1   AA     A
2   BB     B 
> sdf = as.DataFrame(df)
> filter(sdf, str_count(sdf$long, "A") == 2)
Error in if (length(string) == 0) return(character()) : 
  argument is not interpretable as logical

I suspect there is some problem with the type conversions but I cannot find a solution. The subset function and the "array selection" notation fail as well.

Thanks in advance

2

2 Answers

1
votes

To convert normal R data frame to Spark data frame, use this line of code.

sdf <- as.data.frame(df)

And coming to this line of code

filter(sdf, str_count(sdf$long, "A") == 2)

str_count(sdf$long, "A") == 2, this line is perfectly fine and returns c(TRUE, FALSE)

If you want to print data based on the above line, I would suggest to use this line of code

sdf[str_count(sdf$long, "A") == 2] 

which gives you the output (assuming this is your expected result)

  long
1   AA
2   BB

rather than filter(sdf, str_count(sdf$long, "A") == 2), because condition part in filter doesn't accept logical TRUE or FALSE

0
votes

First of all, why do you create "sdf"? df is already a dataframe, and the code you use to declare sdf is not correct (it should be "as.data.frame(df)")

Then, I don't get any error in the filter code you're using.