0

我想按年龄过滤我的参与者。为此,我使用(例如):

patientage <- c(18, 18, 18, 18, 18, 19, 20, 20, 20, 21, 22, 23, 23, 24, 25, 26, 27, 28, 29, 30, 30, 31, 32, 34)

group_by(patientage) %>%  count(patientage) %>% filter(patientage >17 & patientage <31)%>% sum(n)

现在我想用sum(n)18-30 之间的所有参与者来计算。但这会导致错误代码:

> Error in as.vector(x, mode) :    cannot coerce type 'closure' to
> vector of type 'any'

你能帮我吗?非常感谢。

4

1 回答 1

0

首先,dplyr期望数据帧作为输入,所以你必须转换你的向量:

patientage <- data.frame(
  patientage = c(
    18, 18, 18, 18, 18, 19, 20, 20, 20, 21, 22, 23, 23, 24, 25, 26, 27, 
    28, 29, 30, 30, 31, 32, 34
  )
)

现在您可以使用您的数据了。最后,sum()期望再次返回一个向量,这就是为什么您需要pull()在求和之前从数据帧中取出所需的向量:

patientage %>% 
  group_by(patientage) %>% 
  count(patientage) %>% 
  filter(patientage > 17 & patientage < 31) %>% 
  pull(n) %>% 
  sum
于 2019-06-19T10:18:16.513 回答