r- 如何在使用 dplyr 的自定义函数上使用迭代
r- How to use iteration on a custom function that uses dplyr
我想创建一个自定义函数来计算包含 100 多列的大型数据集中的分组百分比。因为我有很多列,所以我想做一个循环或 lapply 或其他东西来避免将函数输入 100 多次。当我为每一列单独输入时,我编写的函数工作正常,但我无法弄清楚如何重复执行。
这是一个简化的数据框和函数:
# load required libraries:
library(tidyverse)
df<-data.frame(sex=c('M','M','M','F','M','F','M',NA),
school=c('A','A','A','A','B','B','B',NA),
question1=c(NA,1,1,2,2,3,3,3),
question2=c(2,NA,2,4,5,1,2,3))
my_function<-function(dataset,question_number){
question_number_enquo<-enquo(question_number)
dataset%>%
filter(!is.na(!!question_number_enquo)&!is.na(sex))%>%
group_by(school,sex,!!question_number_enquo)%>%
count(!!question_number_enquo)%>%
summarise(number=sum(n))%>%
mutate(percent=number/sum(number)*100)%>%
ungroup()
}
我的函数在我输入列名时起作用:
my_function(df,question1)
A tibble: 5 x 5
school sex question1 number percent
<fct> <fct> <dbl> <int> <dbl>
1 A F 2 1 100
2 A M 1 2 100
3 B F 3 1 100
4 B M 2 1 50
5 B M 3 1 50
以下是我在重复方面的尝试。我想为每一列重复该功能(学校和性别除外,因为它们是我的组)。
question_col_names<-(df%>%select(-sex,-school)%>%colnames())
使用 lapply 和列名作为条件:
question_col_names_enquo<-enquo(question_col_names)
lapply(df,my_function(df,!!question_col_names_enquo))
Error: Column `<chr>` must be length 7 (the number of rows) or one, not 2
尝试 lapply 使用不带引号的列名:
lapply(df,my_function(df,question_col_names))
Error: Column `question_col_names` is unknown
尝试 lapply 引用列名称:
lapply(df,my_function(df,'question_col_names'))
Error: Column `"question_col_names"` can't be modified because it's a grouping variable
我也试过应用,得到了相同类型的错误信息:
apply(df,1,my_function(df,!!question_col_names_enquo))
Error: Column `<chr>` must be length 7 (the number of rows) or one, not 2
apply(df,1,my_function(df,question_col_names))
Error: Column `question_col_names` is unknown
apply(df,1,my_function(df,'question_col_names'))
Error: Column `"question_col_names"` can't be modified because it's a grouping variable
我还尝试了 for 循环的不同变体:
for (i in question_col_names){
my_function(df,i)
}
Error: Column `i` is unknown
for (i in question_col_names){
my_function(df,'i')
}
Error: Column `"i"` can't be modified because it's a grouping variable
如何使用迭代让我的函数重复我的所有列?
我怀疑这与dplyr有关;我知道它在自定义函数中的表现很有趣,但我可以让它在我的函数中工作,而不是在迭代中。我对 Google 和 Stack Overflow 进行了深入研究,但没有找到任何答案。
提前致谢!
你的question_col_names
是字符串。您需要 sym
将字符串转换为函数内的变量而不是
library(tidyverse)
df <- data.frame(
sex = c("M", "M", "M", "F", "M", "F", "M", NA),
school = c("A", "A", "A", "A", "B", "B", "B", NA),
question1 = c(NA, 1, 1, 2, 2, 3, 3, 3),
question2 = c(2, NA, 2, 4, 5, 1, 2, 3)
)
my_function <- function(dataset, question_number) {
question_number_enquo <- sym(question_number)
dataset %>%
filter(!is.na(!!question_number_enquo) & !is.na(sex)) %>%
group_by(school, sex, !!question_number_enquo) %>%
count(!!question_number_enquo) %>%
summarise(number = sum(n)) %>%
mutate(percent = number / sum(number) * 100) %>%
ungroup()
}
my_function(df, "question1")
#> # A tibble: 5 x 5
#> school sex question1 number percent
#> <fct> <fct> <dbl> <int> <dbl>
#> 1 A F 2 1 100
#> 2 A M 1 2 100
#> 3 B F 3 1 100
#> 4 B M 2 1 50
#> 5 B M 3 1 50
question_col_names <- (df %>% select(-sex, -school) %>% colnames())
result <- map_df(question_col_names, ~ my_function(df, .x))
result
#> # A tibble: 10 x 6
#> school sex question1 number percent question2
#> <fct> <fct> <dbl> <int> <dbl> <dbl>
#> 1 A F 2 1 100 NA
#> 2 A M 1 2 100 NA
#> 3 B F 3 1 100 NA
#> 4 B M 2 1 50 NA
#> 5 B M 3 1 50 NA
#> 6 A F NA 1 100 4
#> 7 A M NA 2 100 2
#> 8 B F NA 1 100 1
#> 9 B M NA 1 50 2
#> 10 B M NA 1 50 5
如果将函数结果转换为长格式可能会更好
my_function2 <- function(dataset, question_number) {
question_number_enquo <- sym(question_number)
res <- dataset %>%
filter(!is.na(!!question_number_enquo) & !is.na(sex)) %>%
group_by(school, sex, !!question_number_enquo) %>%
count(!!question_number_enquo) %>%
summarise(number = sum(n)) %>%
mutate(percent = number / sum(number) * 100) %>%
ungroup() %>%
gather(key = 'question', value, -school, -sex, -number, -percent)
return(res)
}
result2 <- map_df(question_col_names, ~ my_function2(df, .x))
result2
#> # A tibble: 10 x 6
#> school sex number percent question value
#> <fct> <fct> <int> <dbl> <chr> <dbl>
#> 1 A F 1 100 question1 2
#> 2 A M 2 100 question1 1
#> 3 B F 1 100 question1 3
#> 4 B M 1 50 question1 2
#> 5 B M 1 50 question1 3
#> 6 A F 1 100 question2 4
#> 7 A M 2 100 question2 2
#> 8 B F 1 100 question2 1
#> 9 B M 1 50 question2 2
#> 10 B M 1 50 question2 5
由 reprex package (v0.3.0)
于 2019-11-25 创建
如果我没理解错的话,你可以使用 gather
、nest
和 map
:
library(tidyverse)
df %>%
rownames_to_column("ID") %>%
gather(question, value, -ID, -sex, -school) %>%
nest(-sex, -school) %>%
mutate(results = purrr::map(data, function(x) {
x %>%
group_by(question)%>%
summarise(number=sum(!is.na(value))) %>%
mutate(percent=number/sum(number)*100)%>%
ungroup()})) %>%
select(sex, school, results) %>%
unnest(results)
结果:
sex school question number percent
<fct> <fct> <chr> <int> <dbl>
1 M A question1 3 50
2 M A question2 3 50
3 F A question1 1 50
4 F A question2 1 50
5 M B question1 2 50
6 M B question2 2 50
7 F B question1 1 50
8 F B question2 1 50
9 NA NA question1 1 50
10 NA NA question2 1 50
我想创建一个自定义函数来计算包含 100 多列的大型数据集中的分组百分比。因为我有很多列,所以我想做一个循环或 lapply 或其他东西来避免将函数输入 100 多次。当我为每一列单独输入时,我编写的函数工作正常,但我无法弄清楚如何重复执行。
这是一个简化的数据框和函数:
# load required libraries:
library(tidyverse)
df<-data.frame(sex=c('M','M','M','F','M','F','M',NA),
school=c('A','A','A','A','B','B','B',NA),
question1=c(NA,1,1,2,2,3,3,3),
question2=c(2,NA,2,4,5,1,2,3))
my_function<-function(dataset,question_number){
question_number_enquo<-enquo(question_number)
dataset%>%
filter(!is.na(!!question_number_enquo)&!is.na(sex))%>%
group_by(school,sex,!!question_number_enquo)%>%
count(!!question_number_enquo)%>%
summarise(number=sum(n))%>%
mutate(percent=number/sum(number)*100)%>%
ungroup()
}
我的函数在我输入列名时起作用:
my_function(df,question1)
A tibble: 5 x 5
school sex question1 number percent
<fct> <fct> <dbl> <int> <dbl>
1 A F 2 1 100
2 A M 1 2 100
3 B F 3 1 100
4 B M 2 1 50
5 B M 3 1 50
以下是我在重复方面的尝试。我想为每一列重复该功能(学校和性别除外,因为它们是我的组)。
question_col_names<-(df%>%select(-sex,-school)%>%colnames())
使用 lapply 和列名作为条件:
question_col_names_enquo<-enquo(question_col_names)
lapply(df,my_function(df,!!question_col_names_enquo))
Error: Column `<chr>` must be length 7 (the number of rows) or one, not 2
尝试 lapply 使用不带引号的列名:
lapply(df,my_function(df,question_col_names))
Error: Column `question_col_names` is unknown
尝试 lapply 引用列名称:
lapply(df,my_function(df,'question_col_names'))
Error: Column `"question_col_names"` can't be modified because it's a grouping variable
我也试过应用,得到了相同类型的错误信息:
apply(df,1,my_function(df,!!question_col_names_enquo))
Error: Column `<chr>` must be length 7 (the number of rows) or one, not 2
apply(df,1,my_function(df,question_col_names))
Error: Column `question_col_names` is unknown
apply(df,1,my_function(df,'question_col_names'))
Error: Column `"question_col_names"` can't be modified because it's a grouping variable
我还尝试了 for 循环的不同变体:
for (i in question_col_names){
my_function(df,i)
}
Error: Column `i` is unknown
for (i in question_col_names){
my_function(df,'i')
}
Error: Column `"i"` can't be modified because it's a grouping variable
如何使用迭代让我的函数重复我的所有列?
我怀疑这与dplyr有关;我知道它在自定义函数中的表现很有趣,但我可以让它在我的函数中工作,而不是在迭代中。我对 Google 和 Stack Overflow 进行了深入研究,但没有找到任何答案。
提前致谢!
你的question_col_names
是字符串。您需要 sym
将字符串转换为函数内的变量而不是
library(tidyverse)
df <- data.frame(
sex = c("M", "M", "M", "F", "M", "F", "M", NA),
school = c("A", "A", "A", "A", "B", "B", "B", NA),
question1 = c(NA, 1, 1, 2, 2, 3, 3, 3),
question2 = c(2, NA, 2, 4, 5, 1, 2, 3)
)
my_function <- function(dataset, question_number) {
question_number_enquo <- sym(question_number)
dataset %>%
filter(!is.na(!!question_number_enquo) & !is.na(sex)) %>%
group_by(school, sex, !!question_number_enquo) %>%
count(!!question_number_enquo) %>%
summarise(number = sum(n)) %>%
mutate(percent = number / sum(number) * 100) %>%
ungroup()
}
my_function(df, "question1")
#> # A tibble: 5 x 5
#> school sex question1 number percent
#> <fct> <fct> <dbl> <int> <dbl>
#> 1 A F 2 1 100
#> 2 A M 1 2 100
#> 3 B F 3 1 100
#> 4 B M 2 1 50
#> 5 B M 3 1 50
question_col_names <- (df %>% select(-sex, -school) %>% colnames())
result <- map_df(question_col_names, ~ my_function(df, .x))
result
#> # A tibble: 10 x 6
#> school sex question1 number percent question2
#> <fct> <fct> <dbl> <int> <dbl> <dbl>
#> 1 A F 2 1 100 NA
#> 2 A M 1 2 100 NA
#> 3 B F 3 1 100 NA
#> 4 B M 2 1 50 NA
#> 5 B M 3 1 50 NA
#> 6 A F NA 1 100 4
#> 7 A M NA 2 100 2
#> 8 B F NA 1 100 1
#> 9 B M NA 1 50 2
#> 10 B M NA 1 50 5
如果将函数结果转换为长格式可能会更好
my_function2 <- function(dataset, question_number) {
question_number_enquo <- sym(question_number)
res <- dataset %>%
filter(!is.na(!!question_number_enquo) & !is.na(sex)) %>%
group_by(school, sex, !!question_number_enquo) %>%
count(!!question_number_enquo) %>%
summarise(number = sum(n)) %>%
mutate(percent = number / sum(number) * 100) %>%
ungroup() %>%
gather(key = 'question', value, -school, -sex, -number, -percent)
return(res)
}
result2 <- map_df(question_col_names, ~ my_function2(df, .x))
result2
#> # A tibble: 10 x 6
#> school sex number percent question value
#> <fct> <fct> <int> <dbl> <chr> <dbl>
#> 1 A F 1 100 question1 2
#> 2 A M 2 100 question1 1
#> 3 B F 1 100 question1 3
#> 4 B M 1 50 question1 2
#> 5 B M 1 50 question1 3
#> 6 A F 1 100 question2 4
#> 7 A M 2 100 question2 2
#> 8 B F 1 100 question2 1
#> 9 B M 1 50 question2 2
#> 10 B M 1 50 question2 5
由 reprex package (v0.3.0)
于 2019-11-25 创建如果我没理解错的话,你可以使用 gather
、nest
和 map
:
library(tidyverse)
df %>%
rownames_to_column("ID") %>%
gather(question, value, -ID, -sex, -school) %>%
nest(-sex, -school) %>%
mutate(results = purrr::map(data, function(x) {
x %>%
group_by(question)%>%
summarise(number=sum(!is.na(value))) %>%
mutate(percent=number/sum(number)*100)%>%
ungroup()})) %>%
select(sex, school, results) %>%
unnest(results)
结果:
sex school question number percent
<fct> <fct> <chr> <int> <dbl>
1 M A question1 3 50
2 M A question2 3 50
3 F A question1 1 50
4 F A question2 1 50
5 M B question1 2 50
6 M B question2 2 50
7 F B question1 1 50
8 F B question2 1 50
9 NA NA question1 1 50
10 NA NA question2 1 50