dplyr错误:R中的length(rows)== 1不是TRUE [英] dplyr Error: length(rows) == 1 is not TRUE in R

查看:431
本文介绍了dplyr错误:R中的length(rows)== 1不是TRUE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在某些背景下,我正在使用的数据来自某些变量的前3名.我需要能够计算1s,2s,3s和NA(在前3名中未包括在内的#ppl).

As some background, the data I'm working with is from ranking top 3 of certain variables. I need to be able to count the 1s, 2s,3s, and the NAs (# ppl who did not include it in the top 3).

我有我的数据框LikelyRenew_ReasonB,我使用dplyr过滤了特定的年份和状态,可以正常工作/没有错误.

I have my data frame LikelyRenew_ReasonB and I used dplyr to filter for a particular year and status, which works correctly/no errors.

LikelyRenew_ReasonB <-    
  LikelyRenew_Reason %>%
      filter(year ==1, status ==2)

> LikelyRenew_ReasonB
  cost products commun reimburse policy discount status year
1   NA       NA     NA        NA     NA       NA      2    1
2   NA       NA      1         2     NA       NA      2    1
3    2       NA      3        NA      1       NA      2    1
4   NA       NA     NA         1     NA       NA      2    1
5   NA       NA      3         1      2       NA      2    1
6   NA       NA      2         1      3       NA      2    1
7   NA       NA      1        NA     NA       NA      2    1
8   NA        2      3         1     NA       NA      2    1
9    3       NA      1        NA      2       NA      2    1

但是,当我尝试获取摘要计数时,它会引发错误:错误:R中的length(rows)== 1不是TRUE.我不知道为什么会收到此错误,如果更改过滤器,则更是如此到year == 3,status == 1,则工作正常.关于我在这里缺少什么的任何想法?

However, when I try to get summary counts it throws the error: Error: length(rows) == 1 is not TRUE in R. I don't know why I get this error, and further if I change my filter to year ==3, status==1, then it works fine. Any ideas on what I am missing here?

    LikelyRenew_ReasonB  %>%
          summarize(
            costC = count(cost), 
            productsC = count(products),
            communC = count(commun),
            reimburseC = count(reimburse),
            policyC = count(policy),
            discountC = count(discount))

这是LikelyRenew_ReasonB的样子(*请注意,这是当我将year == 3,status == 1作为过滤器时的dput头)

Here is what LikelyRenew_ReasonB looks like (*please note this is the dput head following when I have year ==3, status ==1 as the filter)

> dput(head(LikelyRenew_ReasonB))
structure(list(costC = structure(list(x = c(1, 2, 3, NA), freq = c(10L, 
11L, 17L, 149L)), .Names = c("x", "freq"), row.names = c(NA, 
4L), class = "data.frame"), productsC = structure(list(x = c(1, 
2, 3, NA), freq = c(31L, 40L, 30L, 86L)), .Names = c("x", "freq"
), row.names = c(NA, 4L), class = "data.frame"), communC = structure(list(
x = c(1, 2, 3, NA), freq = c(51L, 50L, 34L, 52L)), .Names = c("x", 
"freq"), row.names = c(NA, 4L), class = "data.frame"), reimburseC = 
structure(list(
x = c(1, 2, 3, NA), freq = c(42L, 26L, 25L, 94L)), .Names = c("x", 
"freq"), row.names = c(NA, 4L), class = "data.frame"), policyC = 
structure(list(
x = c(1, 2, 3, NA), freq = c(31L, 25L, 28L, 103L)), .Names = c("x", 
"freq"), row.names = c(NA, 4L), class = "data.frame"), discountC = 
structure(list(
x = c(1, 2, 3, NA), freq = c(2L, 2L, 3L, 180L)), .Names = c("x", 
 "freq"), row.names = c(NA, 4L), class = "data.frame")), .Names = c("costC", 
"productsC", "communC", "reimburseC", "policyC", "discountC"), row.names = 
c(NA, 
 4L), class = "data.frame")

这是工作"的一个例子.同样,问题是由于某种原因,当我将状态/年份更改为其他感兴趣的细分时,我得到了一个错误.

Here is an example of it 'working'. Again, the problem is for some reason I get an error when I change the status/year to a different segment of interest.

> LikelyRenew_ReasonB <-    
+   LikelyRenew_Reason %>%
+   dplyr::filter(year ==3, status ==1) %>%
+   plyr::summarize(
+                 costC = count(cost), 
+                 productsC = count(products),
+                 communC = count(commun),
+                 reimburseC = count(reimburse),
+                 policyC = count(policy),
+                 discountC = count(discount))

以下是正确输出的示例

    > LikelyRenew_ReasonB
    costC.x costC.freq productsC.x productsC.freq
1       1         10           1             31
2       2         11           2             40
3       3         17           3             30
4      NA        149          NA             86

推荐答案

Count()是summarise()的包装

Count() is a wrapper for summarise() https://dplyr.tidyverse.org/reference/tally.html. Perhaps what you want is to use sum() instead of count()?

LikelyRenew_ReasonB %>%
    summarize(
        costC = sum(cost, na.rm = TRUE),
        productsC = sum(products, na.rm = TRUE),
        communC = sum(commun, na.rm = TRUE),
        reimburseC = sum(reimburse, na.rm = TRUE),
        policyC = sum(policy, na.rm = TRUE),
        discountC = sum(discount, na.rm = TRUE))

这篇关于dplyr错误:R中的length(rows)== 1不是TRUE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆