我想按3个变量分组,并使用汇总函数创建新变量。
我的代码:
备选案文1
library(tidyverse)
library(dplyr)
example2<-example%>%
group_by(age_cohort,sex,city)%>%
summarise(rich=sum(rich),
middleclass=sum(middleclass),
poor=sum(poor),
population=count(id))
我不明白这个错误:
Error in `summarise()`:
! Problem while computing `population = count(id)`.
i The error occurred in group 1: age_cohort = 1, sex = 0, city = 1.
Caused by error in `UseMethod()`:
! no applicable method for 'count' applied to an object of class "c('double', 'numeric')"
Run `rlang::last_error()` to see where the error occurred.
选项2
example3<-example%>%
group_by(age_cohort,sex,city)%>%
summarise(rich=sum(rich),
middleclass=sum(middleclass),
poor=sum(poor),
population=n(id))
错误:
Error in `summarise()`:
! Problem while computing `population = n(id)`.
i The error occurred in group 1: age_cohort = 1, sex = 0, city = 1.
Caused by error in `n()`:
! unused argument (id)
Run `rlang::last_error()` to see where the error occurred.
此外,如果我删除了“population”变量,那么我的代码仍然有问题。
新码
example<-example%>%
group_by(age_cohort,sex,city)%>%
summarise(rich=sum(rich),
middleclass=sum(middleclass),
poor=sum(poor))
错误:
Error in UseMethod("group_by") :
no applicable method for 'group_by' applied to an object of class "function"
原始数据(示例):
id sex city rich middleclass poor age_cohort
1 0 1 1 0 0 1
2 1 1 0 1 0 5
3 1 2 0 0 1 2
4 0 2 0 0 1 3
5 1 3 0 0 1 4
6 0 4 0 1 0 1
7 0 6 0 1 0 1
8 1 7 1 0 0 5
9 0 3 1 0 0 5
10 1 7 0 1 5
11 1 3 0 0 1 2
12 1 1 0 0 1 3
发布于 2022-04-07 07:52:56
正如akrun所说,您需要population=n()
。
id <- 1:12
sex <- c(0,1,1,0,1,0,0,1,0,1,1,1)
city <- c(1,1,2,2,3,4,6,7,3,7,3,1)
rich <- c(1,0,0,0,0,0,0,1,1,0,0,0)
middleclass <- c(0,1,0,0,0,1,1,0,0,1,0,0)
poor <- c(0,0,1,1,1,0,0,0,0,NA,1,1)
age_cohort <- c(1,5,2,3,4,1,1,5,5,5,2,3)
example <- data.frame(id,sex,city,rich,middleclass,poor,age_cohort)
example3 <- example%>%
group_by(age_cohort,sex,city)%>%
summarise(rich=sum(rich),
middleclass=sum(middleclass),
poor=sum(poor),
population=n())
输出
> example
id sex city rich middleclass poor age_cohort
1 1 0 1 1 0 0 1
2 2 1 1 0 1 0 5
3 3 1 2 0 0 1 2
4 4 0 2 0 0 1 3
5 5 1 3 0 0 1 4
6 6 0 4 0 1 0 1
7 7 0 6 0 1 0 1
8 8 1 7 1 0 0 5
9 9 0 3 1 0 0 5
10 10 1 7 0 1 NA 5
11 11 1 3 0 0 1 2
12 12 1 1 0 0 1 3
> example3
# A tibble: 11 x 7
# Groups: age_cohort, sex [7]
age_cohort sex city rich middleclass poor population
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <int>
1 1 0 1 1 0 0 1
2 1 0 4 0 1 0 1
3 1 0 6 0 1 0 1
4 2 1 2 0 0 1 1
5 2 1 3 0 0 1 1
6 3 0 2 0 0 1 1
7 3 1 1 0 0 1 1
8 4 1 3 0 0 1 1
9 5 0 3 1 0 0 1
10 5 1 1 0 1 0 1
11 5 1 7 1 1 NA 2
为什么有错误
正如其他人在评论中指出的那样。
第一个错误是由于count
处理数据格式和变量名造成的;它不能用作摘要函数。例如,count(example, sex)
。您给了count
一个数字向量(an object of class "c('double', 'numeric')
),它不能接受它作为参数(no applicable method for 'count' applied to...
)。
第二个错误是由于n()
只返回关于最后一个分组变量的信息(参见?context
)。这一次,您给了它一个参数,但是它不需要任何参数,因为最后一个分组变量是由group_by
指定的,所以它返回了unused argument
。
最后一个错误是由于您在执行example
之前没有在环境中创建对象group_by
。实际上,example
是utils
中函数的名称(参见?example
)。因此,如果您不创建一个具有该名称的对象,R认为您所指的是名为example
的函数。然后尝试group
,而R不能这样做,因为它只对数据文件起作用。当类函数(an object of class "function"
)需要数据时,您给了它一个参数。
https://stackoverflow.com/questions/71769441
复制相似问题