作为一些背景,我使用的数据来自某些变量的前三名。我需要能够计算1s,2s,3s和NA(#ppl谁没有将它包含在前3名中)。
我有我的数据框LikelyRenew_ReasonB,我使用dplyr来过滤特定的年份和状态,它正常工作/没有错误。
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
然而,当我尝试获取摘要计数时,它会抛出错误:错误:长度(行)== 1在R中不为TRUE。我不知道为什么会出现此错误,如果我改变了我的过滤器年份== 3,状态== 1,然后它工作正常。关于我在这里缺少什么的想法?
LikelyRenew_ReasonB %>%
summarize(
costC = count(cost),
productsC = count(products),
communC = count(commun),
reimburseC = count(reimburse),
policyC = count(policy),
discountC = count(discount))
这是LikelyRenew_ReasonB的样子(*请注意这是当我有年= = 3,状态== 1作为过滤器时的输入头)
> 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")
这是一个工作&#39;的例子。同样,问题是出于某种原因,当我将状态/年更改为不同的兴趣段时,我收到错误。
> 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
答案 0 :(得分:-1)
Count()是summarize()https://dplyr.tidyverse.org/reference/tally.html的包装器。也许你想要的是使用sum()而不是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))