-2

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
Ellie
  • 415
  • 7
  • 16
  • Sorry, I've been fiddling with it trying to figure it out – Ellie May 16 '18 at 17:16
  • > dput(head(LikelyRenew_Reason)) structure(list(cost = c(3, NA, NA, NA, NA, 3), products = c(2, NA, NA, 3, 3, 2), commun = c(1, 1, 2, 1, 2, 1), reimburse = c(NA, 2, 1, NA, NA, NA), policy = c(NA, NA, 3, NA, 1, NA), discount = c(NA, 3, NA, 2, NA, NA), status = c(5, 5, 1, 5, 1, 1), year = c(3, 3, 3, 3, 3, 3)),codepage = 65001L, .Names = c("cost", "products", "commun", "reimburse", "policy", "discount", "status", "year"), row.names = c(NA, 6L), class = "data.frame") – Ellie May 16 '18 at 17:20
  • all of dput(head(LikelyRenew_Reason)) is like 1500 lines. Taking out the part that is the variable.labels = structure(c(...)) leaves me with the above if that's at all helpful – Ellie May 16 '18 at 17:21
  • OK, originally, you _didn't_ mention that you were using `plyr::summarize` instead of `dplyr::summarize`, so it is very reasonable for us to assume you were trying to use `dplyr::summarize`. Next time when asking a question, please always include the packages you are using, and which functions are from which package. Otherwise it will be very confusing. – acylam May 16 '18 at 18:02

1 Answers1

-1

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))
SGY
  • 21
  • 4