R: Error in UseMethod(“tbl_vars”)

别来无恙 提交于 2020-05-13 20:01:38

问题


So I'm running the code below in R Studio and getting this error:

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

I don't know how to fix it cause there is no tbl_vars function! Can someone help?

for (i in 1:ceiling(nrow(reviews)/batch)) {
    row_start <- i*batch-batch+1

    row_end <- ifelse(i*batch < nrow(reviews), i*batch, nrow(reviews))

    print(paste("Processing row", row_start, "to row", row_end))

    reviews[row_start:row_end, ] %>%
        unnest_tokens(word, text) -> reviews_subset

    reviews_subset$row <- 1:nrow(reviews_subset)

    reviews_subset %>%
        anti_join(stopwords) %>%
        arrange(row) -> reviews_subset

    write_feather(reviews_subset, path = paste0("reviews", i, ".txt"))
}

Ps: dplyr is installed. Also other installed packages: pacman, feather, data.table, devtools, tidyr, tidytext, tokenizers, tibble

I'm using it to work with Yelp dataset.

Thank you so much, Carmem

ps2: dataset example (edited and simplified to fit here):

> dput(as.data.frame(review))

structure(list(user_id = 1:10, review_id = 11:20, business_id = 21:30, 
    stars = c(2L, 2L, 5L, 4L, 4L, 5L, 4L, 3L, 5L, 4L), text = c("Are you the type of person that requires being seen in an expensive, overly pretentious restaurant so that you can wear it as a status symbol?  Or maybe you're a gansta who dresses like CiLo Green and wants to show the hunny's (yes, a group of them out with one man) a night on the town!", 
    "Today was my first visit to the new luna, and I was disappointed-- both because I really liked the old cafe luna, and because the new luna came well recommended", 
    "Stayed here a few months ago and still remember the great service I received.", 
    "I came here for a business lunch from NYC and had a VERY appetizing meal. ", 
    "Incredible food with great flavor. ", 
    "OMG, y'all, try the Apple Pie Moonshine.  It. Is. Seriously. Good.  Smoooooooth.   The best rum that I've sampled so far: Zaya.", 
    "Caitlin is an amazing stylist.  She took time to hear what I had to say before jumping in", 
    "Oh yeah! After some difficulties in securing dinner, my dad and I found ourselves at one of the billion Primanti's locations for a quick feast", 
    "I've been going to this studio since the beginning of January", 
    "The best cannoli, hands down!!"
    )), .Names = c("user_id", "review_id", "business_id", "stars", 
"text"), row.names = c(NA, -10L), class = "data.frame")

回答1:


change anti_join(stopwords) to anti_join(stop_words). stopwords probably doesn't exist or isn't what you want it to be




回答2:


There are several ways to resolve this error. As Szczepaniak points out, the root cause is attempting to pass a character vector into an operation that expects a data frame or tibble.

Option 1: convert the character vector to a data frame (or tibble), then use in anti_join. An example conversion:

        `stopwords <- tibble(joinColumn = stopwords)`  

Option 2: change the operation to accept a character vector. In this case we can use filter in place of anti_join as shown here:

         `reviews_subset <- reviews_subset %>%
              filter(!joinColumn %in% stopwords) %>%
              arrange(row) -> reviews_subset`



回答3:


The

Error in UseMethod("tbl_vars") : no applicable method for 'tbl_vars'...

message is not being caused by a missing tbl_vars function. I ran into this exact same error when I mistakenly passed a vector to a dplyr join function instead of another dataframe. Here is a simple example of how to generate this error in R 3.5 using dplyr 0.7.5:

library(dplyr)
# Create a dataframe of sales by person and bike color
salesNames = c('Sally', 'Jim', 'Chris', 'Chris', 'Jim',
               'Sally', 'Jim', 'Sally', 'Chris', 'Sally')

salesDates = c('2018-06-01', '2018-06-05', '2018-06-10', '2018-06-15',
               '2018-06-20', '2018-06-25', '2018-06-30', '2018-07-09',
               '2018-07-12', '2018-07-14')

salesColor = c('red', 'red', 'red', 'green', 'red',
               'blue', 'green', 'green', 'green', 'blue')

df_sales = data.frame(Salesperson = salesNames,
                      SalesDate = as.Date(salesDates),
                      BikeColor = salesColor,
                      stringsAsFactors = F)

# Create another dataframe to join to
modelColor = c('red', 'blue', 'green', 'yellow', 'orange', 'black')
modelPrice = c(279.95, 269.95, 264.95, 233.54, 255.27, 289.95)
modelCommission = modelPrice * 0.20

df_commissions = data.frame(ModelColor = modelColor,
                            ModelPrice = modelPrice,
                            Commission = modelCommission,
                            stringsAsFactors = F)

df_sales_comm = df_sales %>% left_join(df_commissions,
                                       by = c('BikeColor'= 'ModelColor'))

This works fine. Now try this:

df_comms = df_commissions$ModelColor  # vector instead of dataframe

df_sales_comm2 = df_sales %>% left_join(df_comms,
                                        by = c('BikeColor'= 'ModelColor'))

and you should see the exact same error you report because df_comms is not a dateframe. The problem you are having is that stopwords is a vector and not a dataframe (or a tibble).



来源:https://stackoverflow.com/questions/50489511/r-error-in-usemethodtbl-vars

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!