1

所以这个函数的目标是获取一系列分类变量并使用组/类变量运行 chisq 测试,然后打印出一个包含所有摘要信息的表。

当我(在 stackoverflow 的帮助下)为单个变量构建函数时,它打印得非常完美。但是后来我使用 rowbind/lapply 将所有变量的结果放在一起,现在它代替了变量名,它给了我在变量列表中的位置。我尝试了其他一些选项,它们要么使该功能无法运行,要么打印出相同外观的输出。

是否有不同的论点:

res[1,1] <- deparse(substitute(cat)) 

或者

do.call(rbind.data.frame,lapply(data[,catvars],...

这将使我能够打印出变量名?

#Some practice data
get.data<-function(){
set.seed(1)
cat1 <-sample(c(1,2), 100, replace=T)
cont1<-rnorm(100, 25, 8)
cont2<-rnorm(100, 0, 1)
cont3<-rnorm(100, 6, 14.23)
cont4<-rnorm(100, 25, 8)*runif(5, 0.1, 1)
cat2<-sample(c(1,2,3,4),100,replace=TRUE)
cat3<-sample(c(1,2,3,4,5),100,replace=TRUE)
cat4<-sample(c("Caucasian","African American", "Latino", "Multi-Racial", "No   
 Response"),100,replace=TRUE)
group<-sample(c(0,1), 100, replace=T)
sex<-sample(c("male", "female"), 100, replace=T)
one  <<-data.frame(group, sex,cat1, cont1, cont2, cont3, cont4,cat2,cat3,cat4)
}

get.data()

#function
make.table<-function(catvars,group,data){
attach(data)
get.chi.stuff<-function(cat, group){
long <- table(cat,group)
test<-chisq.test(cat,group)
kk<-c(test$statistic,test$p.value,test$method)
res <- data.frame(matrix(NA,nrow(long),7))
names(res) <- c("Variable", "Response", "Group1.Freq", "Group2.Freq",
"Test.Stat", "p.value", "method")
res[1,1] <- deparse(substitute(cat))
res[,2] <- row.names(long)
res[,3:4] <- long[,1:2]
res[1,5:7] <- kk
return(res)
}
tabless<<-do.call(rbind.data.frame,lapply(data[,catvars],get.chi.stuff,group=group))

detach(data)

}

#list of variables of interest
catvars<-c("cat1", "cat2", "cat3","cat4")
#call to function
make.table(catvars=catvars,group=group, data=one) 



 X[[1L]]   1   26   26   0.40    0.52   Pearson's Chi-squared test  
 NA        2   28   20   NA      NA   NA
 X[[2L]]   1   17   12   1.16    0.76    Pearson's Chi-squared test
 NA        2   11   13   NA      NA   NA
 NA        3   13   9    NA      NA   NA
 NA        4   13   12   NA      NA   NA

我希望它看起来像:

 cat1      1   26   26   0.40    0.52   Pearson's Chi-squared test  
 NA        2   28   20   NA      NA   NA
 cat2      1   17   12   1.16    0.76    Pearson's Chi-squared test
 NA        2   11   13   NA      NA   NA
 NA        3   13   9    NA      NA   NA
 NA        4   13   12   NA      NA   NA
4

1 回答 1

0

我的部分/骗子解决方法的洞察力。我刚刚创建了第二个 data.frame,其中包含来自 catvars 的 cat 变量的名称以及列表中的位置,然后通过几个步骤,正确地将两个 data.frame 合并在一起并删除了额外的列。

#function
make.table<-function(catvars,group,data){
  attach(data)
  get.chi.stuff<-function(cat, group){
    long <- table(cat,group)
    test<-chisq.test(cat,group)
    kk<-c(test$statistic,test$p.value,test$method)
    res <- data.frame(matrix(NA,nrow(long),7))
    names(res) <- c("list", "Response", "Group1.Freq", "Group2.Freq",
                "Test.Stat", "p.value", "method")
    res[1,1] <- deparse(substitute(cat))
    res[,2] <- row.names(long)
    res[,3:4] <- long[,1:2]
    res[1,5:7] <- kk
    return(res)
  }

  tabless<-do.call(rbind.data.frame,lapply(data[,catvars],get.chi.stuff,group=group))
  length<-1:length(tabless[,1])
  table2<<-data.frame(length,tabless)
  detach(data)

}

#list of variables of interest
catvars<-c("cat1", "cat2", "cat3","cat4")
#call to function
make.table(catvars=catvars,group=group, data=one) 

Variable<-catvars
numbersss<-match(catvars,catvars)
list<-gsub("\\s","",paste("X[[",numbersss,"L]]"))
matched<-data.frame(list,Variable)
newtable<-merge(matched,table2,by="list", all=TRUE)
finaltable<-newtable[with(newtable, order(length)), ]
drops <- c("row.names", "list", "length")
cattable<-finaltable[,!(names(finaltable) %in% drops)]




Variable  Response  Group1.Freq   Group2.Freq    Test.Stat    p.value     method
cat1        1       26        26              0.40         0.52      Pearson's Chi-squared test with Yates' continuity correction
NA          2       28        20              NA            NA         NA
cat2        1       17        12              1.16          0.76       Pearson's Chi-squared test
NA          2       11        13              NA            NA         NA
NA          3       13         9              NA            NA         NA
NA          4       13            12             NA            NA
于 2014-03-18T17:26:30.740 回答