我想为数据框中的多个项目创建列联表并运行chisq.test()等。
各种尝试均导致“表(y $ x,y $ q2)中出现错误:所有参数的长度必须相同”。
我认为以下示例着重于我的中心问题,尽管最终我会编写一个更复杂的函数。我会对我的特定功能或整体方法的解决方案感兴趣。谢谢!
my_df <- structure(list(q1 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L),
.Label = c("Choice1", "Choice2"),
class = "factor"),
q2 = structure(c(1L, 1L, 4L, 5L, 4L, 1L, 1L, 4L),
.Label = c("Agree", "Disagree","N/A or No Opinion",
"Strongly Agree", "Strongly Disagree"),
class = "factor"),
q3 = structure(c(1L, 4L, 1L, 4L, 1L, 4L, 4L, 4L),
.Label = c("Agree", "Disagree","N/A or No Opinion",
"Strongly Agree", "Strongly Disagree"),
class = "factor")),
row.names = c(NA, -8L),
class = c("tbl_df", "tbl", "data.frame"))
my_fn <- function(x, y) {
table(y$x, y$`q2`)
}
my_fn(names(my_df)[1], my_df)
#Error in table(y$x, y$q2) : all arguments must have the same length
lapply(names(my_df), my_fn, my_df)
#Error in table(y$x, y$q2) : all arguments must have the same length
答案 0 :(得分:1)
尝试一下。该问题可能与对变量使用$
有关。如果要使用名称,最好使用[[]]
,以便函数可以理解名称的字符串。这里的代码,对您的函数做了些微更改。我添加了一些示例:
#Function
my_fn <- function(x, y) {
table(y[[x]], y[['q2']])
}
#Code
my_fn('q1', my_df)
lapply(names(my_df),my_fn,y=my_df)
输出:
[[1]]
Agree Disagree N/A or No Opinion Strongly Agree Strongly Disagree
Choice1 4 0 0 3 1
Choice2 0 0 0 0 0
[[2]]
Agree Disagree N/A or No Opinion Strongly Agree Strongly Disagree
Agree 4 0 0 0 0
Disagree 0 0 0 0 0
N/A or No Opinion 0 0 0 0 0
Strongly Agree 0 0 0 3 0
Strongly Disagree 0 0 0 0 1
[[3]]
Agree Disagree N/A or No Opinion Strongly Agree Strongly Disagree
Agree 1 0 0 2 0
Disagree 0 0 0 0 0
N/A or No Opinion 0 0 0 0 0
Strongly Agree 3 0 0 1 1
Strongly Disagree 0 0 0 0 0
答案 1 :(得分:0)
我们可以使用count
中的dplyr
。它将以小标题格式获取数据
library(dplyr)
library(purrr)
my_fn <- function(data, col1) {data %>%
count(!! rlang::sym(col1), q2)}
map(names(my_df), ~ my_fn(my_df, .x))
#[[1]]
# A tibble: 3 x 3
# q1 q2 n
# <fct> <fct> <int>
#1 Choice1 Agree 4
#2 Choice1 Strongly Agree 3
#3 Choice1 Strongly Disagree 1
#[[2]]
# A tibble: 3 x 2
# q2 n
# <fct> <int>
#1 Agree 4
#2 Strongly Agree 3
#3 Strongly Disagree 1
#[[3]]
# A tibble: 5 x 3
# q3 q2 n
# <fct> <fct> <int>
#1 Agree Agree 1
#2 Agree Strongly Agree 2
#3 Strongly Agree Agree 3
#4 Strongly Agree Strongly Agree 1
#5 Strongly Agree Strongly Disagree 1