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