Question

So the goal of this function is to take a series of categorical variables and run chisq tests with a group/class variable and then print out a table that has summary info for all.

When I (with help from stackoverflow) was building the function for a single variable it printed out just perfectly. But then I used a rowbind/lapply to put the results for all variables together and now instead of the variable name, it gives me the position within the variable list. I've tried some other options and they either make the function inoperable or printed the same looking output.

Is there a different argument for either:

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

or

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

that will enable me to have the variable name printed off?

#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

And I'd like it to look like:

 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
Was it helpful?

Solution

Stroke of insight on my part/cheaters work-around. I just created a second data.frame that had the names of the cat variables from catvars, and the position within the list, and then with a few steps, correctly merged the two data.frames together and removed the extra columns.

#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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top