Oliver
Oliver

Reputation: 284

lapply and loop - cannot coerce type 'closure' to vector of type 'character'

I am trying to perform a grepl function on a list of 250 dataframes each with about 50,000 single words in each cell like this:

df1 <- c("this","is","the","first","sentence",...)
df2 <- c("this","is","the","","sentence",...)
df3 <- c("now","the","third","sentence","appears",...)

I want to know how often individual words appear so I decided to put all the dataframes in a list like this:

df_list <- list(df1, df2, df3)

and then I attempted to perform an lapply function on the list:

lapply(all_transcripts, function(x) x %>% filter(grepl("third", word, ignore.case=TRUE)))

However, each time I try to tweak that script I get the same error:

Error in UseMethod("filter_") : no applicable method for 'filter_' applied to an object of class "character"

I then tried to use a loop but got the same error. The loop looked like this:

for (i in 1:length(all_transcripts)) {
   i %>% filter(grepl("third", word, ignore.case=TRUE))
}

I am not wedded to doing this in a particular way. I just want an efficient way of being able to identify how many times certain words are repeated across each the dataframes so if you have another way of doing this, I'm happy to go in a different direction. Really appreciate any help. Thank you in advance!

Upvotes: 1

Views: 307

Answers (1)

Duck
Duck

Reputation: 39595

As it is not clear the final result, these approaches could be tried: In number one:

df1 <- c("this","is","the","first","sentence")
df2 <- c("this","is","the","","sentence")
df3 <- c("now","the","third","sentence","appears")
df_list <- list(df1, df2, df3)
lapply(df_list,function(x) table(x))

[[1]]
x
   first       is sentence      the     this 
       1        1        1        1        1 

[[2]]
x
               is sentence      the     this 
       1        1        1        1        1 

[[3]]
x
 appears      now sentence      the    third 
       1        1        1        1        1 

And number two:

list_g <- do.call(c,df_list)
table(list_g)

list_g
          appears    first       is      now sentence      the    third     this 
       1        1        1        2        1        3        3        1        2 

Hoping this can help.

Upvotes: 3

Related Questions