【问题标题】:dplyr code was working, now isn't, and I'm not sure whydplyr 代码可以工作,现在不行,我不知道为什么
【发布时间】:2019-04-25 12:48:55
【问题描述】:

大约一周前我写了一些代码,效果很好,但现在它停止工作了。我已经更新了 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,只要 data %&gt;% dplyr::count(yearmo, eco) 假设来自其他包的 count 函数没有被屏蔽
  • 现在我收到一个新错误,即:[.data.frame(x, i) 中的错误:选择了未定义的列
  • 你能在新的 R 会话上这样做吗
  • 错误提示 plyr::count ???

标签: r dplyr plyr


【解决方案1】:

显示“数据”后,如果我们使用count,则不需要group_by

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

【讨论】:

  • 这段代码现在可以工作了,我想有一个不同的计数函数弄乱了代码?
  • @hoaxasaurusrex 如果您在我的帖子中使用相同的代码,您的数据是否会出错。这可能是因为count 也存在于其他包中。当你加载一个包时,会有一些消息,来自包 u 的函数 x、y、z 被屏蔽等。
  • @hoaxasaurusrex 另外,不要同时加载plyrdplyr 有很多常用功能,它不起作用
  • 感谢 akrun,这真的很有帮助。不幸的是,我在加载包时总是忽略这些消息——再也不会!
猜你喜欢
  • 2020-09-19
  • 2014-01-17
  • 1970-01-01
  • 2021-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-04
相关资源
最近更新 更多