dplyr代码工作,现在不是,我不知道为什么

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了dplyr代码工作,现在不是,我不知道为什么相关的知识,希望对你有一定的参考价值。

我在一周前编写了一些代码,这些代码效果很好,但现在它已经停止工作了。我已经更新了tidyverse包,看看这可能是问题,但事实并非如此。

以下是一些示例数据:

     yearmo    sex      eco       ue  fs12ago fs12ahead purchases
      200301   Male  neutral negative negative   neutral   neutral
      200301 Female negative negative negative   neutral   neutral
      200301 Female negative negative  neutral   neutral  positive
      200301   Male  neutral  neutral  neutral   neutral   neutral
      200301   Male negative negative negative  positive  negative
      200301   Male negative negative  neutral   neutral  positive
      200301   Male negative negative  neutral   neutral   neutral
      200301   Male negative negative positive   neutral  negative
      200301 Female negative negative negative   neutral  positive
      200301 Female negative negative positive  negative   neutral
      200301 Female negative negative negative  negative  negative
      200301 Female negative  neutral negative   neutral  negative
      200301   Male negative negative  neutral   neutral  negative
      200301 Female positive  neutral  neutral   neutral  positive
      200301   Male negative negative  neutral   neutral  positive
      200301   Male  neutral negative negative   neutral   neutral
      200301 Female  neutral negative negative   neutral   neutral
      200301   Male  neutral negative  neutral   neutral  positive
      200301 Female negative negative negative  negative  positive
      200301 Female positive negative  neutral   neutral  positive

正在运行的代码,但现在不是,这是:

    tmp_eco <- data %>% 
      group_by(yearmo) %>%
      count(yearmo, eco)

我以前获得的输出是对于称为“eco”的变量响应为正,负或中性的人数,例如:

     yearmo     eco         n
      200301  positive     10    
      200301  negative     13
      200301  neutral      9
      200301  positive     7
      200301  negative     5
      200301  neutral      16

我现在得到的错误是:

    Error: Can't subset with `[` using an object of class quoted.
    Call `rlang::last_error()` to see a backtrace

这给了我:

    <error>
    message: Can't subset with `[` using an object of class quoted.
    class:   `rlang_error`
    backtrace:
      1. dplyr::group_by(., yearmo)
      9. plyr::count(., yearmo, eco)
     14. plyr::eval.quoted(vars, df)
     18. tibble:::`[.tbl_df`(envir, exprs)
     19. tibble:::check_names_df(i, x)
    Call `rlang::last_trace()` to see the full backtrace

有关为什么会发生这种情况的任何想法?

答案

随着'数据'显示,如果我们使用group_by,则不需要count

library(dplyr)
data %>% 
   dplyr::count(yearmo, eco)
# A tibble: 3 x 3
#  yearmo eco          n
#   <int> <chr>    <int>
#1 200301 negative    13
#2 200301 neutral      5
#3 200301 positive     2

data

data <- structure(list(yearmo = c(200301L, 200301L, 200301L, 200301L, 
200301L, 200301L, 200301L, 200301L, 200301L, 200301L, 200301L, 
200301L, 200301L, 200301L, 200301L, 200301L, 200301L, 200301L, 
200301L, 200301L), sex = c("Male", "Female", "Female", "Male", 
"Male", "Male", "Male", "Male", "Female", "Female", "Female", 
"Female", "Male", "Female", "Male", "Male", "Female", "Male", 
"Female", "Female"), eco = c("neutral", "negative", "negative", 
"neutral", "negative", "negative", "negative", "negative", "negative", 
"negative", "negative", "negative", "negative", "positive", "negative", 
"neutral", "neutral", "neutral", "negative", "positive"), ue = c("negative", 
"negative", "negative", "neutral", "negative", "negative", "negative", 
"negative", "negative", "negative", "negative", "neutral", "negative", 
"neutral", "negative", "negative", "negative", "negative", "negative", 
"negative"), fs12ago = c("negative", "negative", "neutral", "neutral", 
"negative", "neutral", "neutral", "positive", "negative", "positive", 
"negative", "negative", "neutral", "neutral", "neutral", "negative", 
"negative", "neutral", "negative", "neutral"), fs12ahead = c("neutral", 
"neutral", "neutral", "neutral", "positive", "neutral", "neutral", 
"neutral", "neutral", "negative", "negative", "neutral", "neutral", 
"neutral", "neutral", "neutral", "neutral", "neutral", "negative", 
"neutral"), purchases = c("neutral", "neutral", "positive", "neutral", 
"negative", "positive", "neutral", "negative", "positive", "neutral", 
"negative", "negative", "negative", "positive", "positive", "neutral", 
"neutral", "positive", "positive", "positive")), 
class = "data.frame", row.names = c(NA, 
-20L))

以上是关于dplyr代码工作,现在不是,我不知道为什么的主要内容,如果未能解决你的问题,请参考以下文章

回文字符串问题:为啥我必须放 +1 而不是 -1 才能使这段代码工作?

使用 group_by(多个变量)时的 dplyr 问题

如果 URL 不是 home/root,则 Javascript 代码不起作用

这段代码是如何工作的,很短的方法来反转一个字符串,它工作,但我不知道如何

堆积条形图,按总计(值的总和)重新排序,而不是值 ggplot2 + dplyr

C2653:“GUI”:不是类或命名空间