使用 base::function 与 tidyverse 和 janitor 在 R 中创建多个交叉表
Using base::function with tidyverse and janitor to create several crosstables in R
我有一个包含调查数据的数据框,想在每个维度和诊断之间创建交叉表。我用 dplyr 和看门人成功地做到了这一点:
library(tidyverse)
library(janitor)
survey <- tibble(dimension_1 = c(1, NA, 2, 3, 1, 1, 1, 3, 2, 2),
dimension_2 = c(2, 2, 3, 1, 1, 2, 2, 3, 1, 1),
diagnosis_a = as.factor(c("No", NA, "Yes", "No", "Yes", "Yes", "No", "Yes", "Yes", "Yes")),
diagnosis_b = as.factor(c("Yes", "Yes", "No", NA, "Yes", "No", NA, "Yes", "Yes", "Yes")))
survey %>%
filter(!is.na(diagnosis_a) & !is.na(dimension_1)) %>%
tabyl(dimension_1, diagnosis_a) %>%
adorn_totals() %>%
adorn_percentages() %>%
adorn_pct_formatting() %>%
adorn_ns(position = "front") %>%
adorn_title(row_name = "dimension_1",
col_name = "diagnosis_a")
#> diagnosis_a
#> dimension_1 No Yes
#> 1 2 (50.0%) 2 (50.0%)
#> 2 0 (0.0%) 3 (100.0%)
#> 3 1 (50.0%) 1 (50.0%)
#> Total 3 (33.3%) 6 (66.7%)
由 reprex package (v2.0.1)
创建于 2022-01-05
现在我必须为每个组合复制代码块,并想创建一个函数来将我的变量作为参数传递。不幸的是我无法让它发挥作用。这是我尝试过的一个例子:
xtable_fun <- function(data, dimension, diagnosis) {
data %>%
filter(!is.na(.[diagnosis] & !is.na(.[dimension]))) %>%
tabyl(.[dimension], .[diagnose])
adorn_totals() %>%
adorn_percentages() %>%
adorn_pct_formatting() %>%
adorn_ns(position = "front") %>%
adorn_title(row_name = dimension,
col_name = diagnosis)
}
xtable_fun(survey, "dimension_1", "diagnosis_a")
由 reprex package (v2.0.1)
创建于 2022-01-05
我想错误是我使用参数的方式,但我以前从未尝试过使用我自己的函数,所以找不到解决方案。
感谢您的帮助。
你们非常亲密。尝试使用 .data[[
来引用变量。还有一些错别字:
- 缺少结尾
)
- 缺少
%>%
运算符
- 错误的变量名。
xtable_fun <- function(data, dimension, diagnosis) {
data %>%
filter(!is.na(.data[[diagnosis]]) & !is.na(.data[[dimension]])) %>%
tabyl(.data[[dimension]], .data[[diagnosis]]) %>%
adorn_totals() %>%
adorn_percentages() %>%
adorn_pct_formatting() %>%
adorn_ns(position = "front") %>%
adorn_title(row_name = dimension,
col_name = diagnosis)
}
xtable_fun(survey, "dimension_1", "diagnosis_a")
符合预期。
diagnosis_a
dimension_1 No Yes
1 2 (50.0%) 2 (50.0%)
2 0 (0.0%) 3 (100.0%)
3 1 (50.0%) 1 (50.0%)
Total 3 (33.3%) 6 (66.7%)
我有一个包含调查数据的数据框,想在每个维度和诊断之间创建交叉表。我用 dplyr 和看门人成功地做到了这一点:
library(tidyverse)
library(janitor)
survey <- tibble(dimension_1 = c(1, NA, 2, 3, 1, 1, 1, 3, 2, 2),
dimension_2 = c(2, 2, 3, 1, 1, 2, 2, 3, 1, 1),
diagnosis_a = as.factor(c("No", NA, "Yes", "No", "Yes", "Yes", "No", "Yes", "Yes", "Yes")),
diagnosis_b = as.factor(c("Yes", "Yes", "No", NA, "Yes", "No", NA, "Yes", "Yes", "Yes")))
survey %>%
filter(!is.na(diagnosis_a) & !is.na(dimension_1)) %>%
tabyl(dimension_1, diagnosis_a) %>%
adorn_totals() %>%
adorn_percentages() %>%
adorn_pct_formatting() %>%
adorn_ns(position = "front") %>%
adorn_title(row_name = "dimension_1",
col_name = "diagnosis_a")
#> diagnosis_a
#> dimension_1 No Yes
#> 1 2 (50.0%) 2 (50.0%)
#> 2 0 (0.0%) 3 (100.0%)
#> 3 1 (50.0%) 1 (50.0%)
#> Total 3 (33.3%) 6 (66.7%)
由 reprex package (v2.0.1)
创建于 2022-01-05现在我必须为每个组合复制代码块,并想创建一个函数来将我的变量作为参数传递。不幸的是我无法让它发挥作用。这是我尝试过的一个例子:
xtable_fun <- function(data, dimension, diagnosis) {
data %>%
filter(!is.na(.[diagnosis] & !is.na(.[dimension]))) %>%
tabyl(.[dimension], .[diagnose])
adorn_totals() %>%
adorn_percentages() %>%
adorn_pct_formatting() %>%
adorn_ns(position = "front") %>%
adorn_title(row_name = dimension,
col_name = diagnosis)
}
xtable_fun(survey, "dimension_1", "diagnosis_a")
由 reprex package (v2.0.1)
创建于 2022-01-05我想错误是我使用参数的方式,但我以前从未尝试过使用我自己的函数,所以找不到解决方案。
感谢您的帮助。
你们非常亲密。尝试使用 .data[[
来引用变量。还有一些错别字:
- 缺少结尾
)
- 缺少
%>%
运算符 - 错误的变量名。
xtable_fun <- function(data, dimension, diagnosis) {
data %>%
filter(!is.na(.data[[diagnosis]]) & !is.na(.data[[dimension]])) %>%
tabyl(.data[[dimension]], .data[[diagnosis]]) %>%
adorn_totals() %>%
adorn_percentages() %>%
adorn_pct_formatting() %>%
adorn_ns(position = "front") %>%
adorn_title(row_name = dimension,
col_name = diagnosis)
}
xtable_fun(survey, "dimension_1", "diagnosis_a")
符合预期。
diagnosis_a
dimension_1 No Yes
1 2 (50.0%) 2 (50.0%)
2 0 (0.0%) 3 (100.0%)
3 1 (50.0%) 1 (50.0%)
Total 3 (33.3%) 6 (66.7%)