Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
321 views
in Technique[技术] by (71.8m points)

filter - dplyr Error: length(rows) == 1 is not TRUE in R

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

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

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

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

Here is a sample of the correct output

    > 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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...