【问题标题】:R: Error in UseMethod("tbl_vars")R:使用方法中的错误(“tbl_vars”)
【发布时间】:2018-11-02 12:15:14
【问题描述】:

所以我在 R Studio 中运行以下代码并收到此错误:

UseMethod("tbl_vars") 中的错误:没有适用于 'tbl_vars' 的方法 应用于“字符”类的对象

我不知道如何解决它,因为没有 tbl_vars 函数!有人可以帮忙吗?

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 已安装。还有其他安装的包:pacman、feather、data.table、devtools、tidyr、tidytext、tokenizers、tibble

我正在使用它来处理 Yelp 数据集。

非常感谢, 卡门

ps2:数据集示例(经过编辑和简化以适合此处):

> 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")

【问题讨论】:

  • 您可以发布您的数据样本吗?
  • 数据集是从这里下载的:yelp.com/dataset/download。我不知道如何附上它的样本。如果你告诉我怎么做,我可以做到:)
  • 请参阅here,了解如何发布易于回答的 R 问题。在您的数据子集上使用dput,并将输出发布到您的问题中
  • 我在问题中添加了因为它太大而无法放在这里。顺便说一句,感谢您在此处发布数据的提示:)

标签: r loops dplyr tidytext


【解决方案1】:

有几种方法可以解决此错误。正如 Szczepaniak 指出的那样,根本原因是试图将字符向量传递到需要数据帧或 tibble 的操作中。

选项1:将字符向量转换为数据帧(或tibble),然后在anti_join中使用。转换示例:

        `stopwords <- tibble(joinColumn = stopwords)`  

选项 2:更改操作以接受字符向量。在这种情况下,我们可以使用 filter 代替 anti_join,如下所示:

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

【讨论】:

    【解决方案2】:

    UseMethod("tbl_vars") 中的错误:没有适用于 'tbl_vars' 的方法...

    消息不是由缺少 tbl_vars 函数引起的。当我错误地将向量传递给 dplyr 连接函数而不是另一个数据帧时,我遇到了完全相同的错误。下面是一个简单示例,说明如何使用 dplyr 0.7.5 在 R 3.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'))
    

    这很好用。现在试试这个:

    df_comms = df_commissions$ModelColor  # vector instead of dataframe
    
    df_sales_comm2 = df_sales %>% left_join(df_comms,
                                            by = c('BikeColor'= 'ModelColor'))
    

    并且您应该看到与您报告的完全相同的错误,因为 df_comms 不是日期框。您遇到的问题是 stopwords 是向量而不是数据框(或小标题)。

    【讨论】:

      【解决方案3】:

      anti_join(stopwords) 更改为anti_join(stop_words)stopwords 可能不存在或者不是你想要的样子

      【讨论】:

        猜你喜欢
        • 2020-06-18
        • 2021-09-26
        • 2016-04-10
        • 1970-01-01
        • 1970-01-01
        • 2022-01-25
        • 1970-01-01
        • 1970-01-01
        • 2015-05-23
        相关资源
        最近更新 更多