如何编写一个函数从dataframe列创建多个表?

如何编写一个函数从dataframe列创建多个表?,r,R,我想为数据框中的多个项目创建列联表并运行chisq.test()等 各种尝试都导致了“表中的错误(y$x,y$q2):所有参数必须具有相同的长度” 我认为下面的例子集中在我的中心问题上,尽管最终我会编写一个更复杂的函数。我对我的特定功能或我的整体方法的解决方案感兴趣。谢谢 my_df <- structure(list(q1 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Labe

我想为数据框中的多个项目创建列联表并运行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

试试这个。该问题可能与变量使用
$
有关。如果要使用名称,最好使用
[[]]
,以便函数能够理解名称字符串。这里是代码,对函数稍作修改。我补充了一些例子:

#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)

我们可以使用
dplyr
中的
count
。它将以TIBLE格式获取数据

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
库(dplyr)
图书馆(purrr)
我的_fn%
计数(!!rlang::sym(col1),q2)}
地图(名称(my_-df),~my_-fn(my_-df,.x))
#[[1]]
#一个tibble:3x3
#q1 q2 n
#                  
#1选择1同意4
#2选择1强烈同意3
#3选择1强烈反对1
#[[2]]
#一个tibble:3x2
#q2 n
#               
#1同意4
#2强烈同意3
#3强烈反对1
#[[3]]
#一个tibble:5x3
#第3季度第2季度n
#                         
#我同意
#我非常同意
#3.强烈同意
#强烈同意强烈同意
#5强烈同意强烈不同意1

谢谢-这解决了部分问题。但我不确定是否可以将结果作为chisq.test()或rcompanion::cramerV()的参数传递
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