purrr 循环
df1 <- as.data.frame(df[,4:21])
statistic <- matrix(0, nrow = 3, ncol = 15,
dimnames = list(names(df1[1:3]),names(df1[4:18])))
p <- matrix(0, nrow = 3, ncol = 15,
dimnames = list(names(df1[1:3]),names(df1[4:18])))
for (i in seq(1,3)) {
for (j in seq(4,18)) {
statistic[i,j-3] <- gmodels::CrossTable(df1[,i],df1[,j],chisq = T)$chisq$statistic
p[i,j-3] <- gmodels::CrossTable(df1[,i],df1[,j],chisq = T)$chisq$p.value
}
}
p %<>%as.tibble(p)
item <- function(col){
col <- map_dbl(col, function(x){if(x<0.05){x=0.05} else{x=round(x,2)}})
}
p %<>% map_df(function(x){x=item(x)})
statistic %<>%as.tibble(statistic)
item <- function(col){
col <- map_dbl(col, function(x){x=round(x,2)})
}
statistic %<>% map_df(function(x){x=item(x)})
statistic <- matrix(0, nrow = 3, ncol = 15,
dimnames = list(names(df1[1:3]),names(df1[4:18])))
p <- matrix(0, nrow = 3, ncol = 15,
dimnames = list(names(df1[1:3]),names(df1[4:18])))
for (i in seq(1,3)) {
for (j in seq(4,18)) {
statistic[i,j-3] <- gmodels::CrossTable(df1[,i],df1[,j],chisq = T)$chisq$statistic
p[i,j-3] <- gmodels::CrossTable(df1[,i],df1[,j],chisq = T)$chisq$p.value
}
}
p %<>%as.tibble(p)
item <- function(col){
col <- map_dbl(col, function(x){if(x<0.05){x=0.05} else{x=round(x,2)}})
}
p %<>% map_df(function(x){x=item(x)})
statistic %<>%as.tibble(statistic)
item <- function(col){
col <- map_dbl(col, function(x){x=round(x,2)})
}
statistic %<>% map_df(function(x){x=item(x)})
评论
发表评论