Nathalie
Nathalie

Reputation: 1238

Error when converting the dfm to data frame

I try to run this commands:

library(dplyr)
library(tidyr)
library(quanteda)
 df <- data.frame(id = c(1), text = c("I am loving it"), stringsAsFactors = FALSE)

 myDfm <- df$text %>%
    tokens(remove_punct = TRUE, remove_numbers = TRUE, remove_symbols = TRUE) %>%
     tokens_remove(pattern = c(stopwords(source = "smart"))) %>%
     dfm()


out <- convert(myDfm, to = "data.frame")
pivot_longer(out, cols = !contains("document"), names_to = "features", values_to = "count")  %>% 
    mutate(id = as.integer(gsub("[a-z]", "", document))) %>% 
    inner_join(df) %>% # joins on id
    select(id, features) # select only the id and features column

But I receive this error:

Error: `!contains("document")` must evaluate to column positions or names, not a logical vector
Run `rlang::last_error()` to see where the error occurred.

How is it possible to fix it?

Upvotes: 0

Views: 134

Answers (1)

user13653858
user13653858

Reputation:

Try to use -contains("document") instead of !contains("document") so that it doesn't return a logical vector. So for your case:

pivot_longer(out, cols = -contains("document"), names_to = "features", values_to = "count")  %>% 
    mutate(id = as.integer(gsub("[a-z]", "", document))) %>% 
    inner_join(df) %>% # joins on id
    select(id, features) # select only the id and features column

Note that it depends on package versions, at tidyselect 1.1.0 and dplyr 1.0.0 it runs even with the exclamation mark.

Upvotes: 1

Related Questions