[R] Tables extraction in R ?

Jim Lemon jim at bitwrit.com.au
Sun Jul 8 10:06:22 CEST 2012


On 07/07/2012 03:23 AM, Greeknovice wrote:
> Hi,
> I 'm a novice user of R statistics and my hands-on experience with it is
> minimal.
> I want to create a table for my MBA course assignment that looks like the
> ones that SPSS and MS Excel produces ,the data that the table has to include
> are the following :
>
>> table(agec)
> agec
>    1   2   3
> 749 160  32
>> x=table(agec)
>> x
> agec
>    1   2   3
> 749 160  32
>>
>> prop.table(x)
> agec
>           1          2          3
> 0.79596174 0.17003188 0.03400638
>> prop.test(749,941)
>
>          1-sample proportions test with continuity correction
>
> data:  749 out of 941, null probability 0.5
> X-squared = 328.5186, df = 1, p-value<  2.2e-16
> alternative hypothesis: true p is not equal to 0.5
> 95 percent confidence interval:
>   0.7684801 0.8209873
> sample estimates:
>          p
> 0.7959617
>
>> prop.test(160,941)
>
>          1-sample proportions test with continuity correction
>
> data:  160 out of 941, null probability 0.5
> X-squared = 408.5016, df = 1, p-value<  2.2e-16
> alternative hypothesis: true p is not equal to 0.5
> 95 percent confidence interval:
>   0.1468831 0.1959230
> sample estimates:
>          p
> 0.1700319
>
>> prop.test(32,941)
>
>          1-sample proportions test with continuity correction
>
> data:  32 out of 941, null probability 0.5
> X-squared = 815.4899, df = 1, p-value<  2.2e-16
> alternative hypothesis: true p is not equal to 0.5
> 95 percent confidence interval:
>   0.02374674 0.04822644
> sample estimates:
>           p
> 0.03400638
> This "percentages and confidence intrevals"  table should be  in an image
> file format since I have to upload it to a wiki page.
> Is there a specific command or even a series of commands I can use in order
> to extract this "graphics" table automatically, or I have to create it
> manually using Excel for example?

Hi Greeknovice,
Combining results from different functions into a specified format is a 
common problem in R. As you noted, it has to look like some default 
format used in another system. The flexibility of R allows you to do 
this, but you have to write a function or two like this:

table_with_prop_test<-function(x) {
  counts<-table(x)
  ncounts<-length(counts)
  totalx<-sum(counts)
  pcts<-round(100*counts/totalx,1)
  X2<-df<-p<-lcl<-ucl<-rep(0,ncounts)
  for(i in 1:ncounts) {
   proptest<-prop.test(counts[i],totalx)
   X2[i]<-round(proptest$statistic,2)
   df[i]<-proptest$parameter
   p[i]<-round(proptest$p.value,3)
   lcl[i]<-round(proptest$conf.int[1],3)
   ucl[i]<-round(proptest$conf.int[2],3)
  }
  tptmat<-cbind(counts,pcts,X2,df,p,lcl,ucl)
  return(tptmat)
}

Then if you want to turn the result into an image, you can do something 
like this:

library(plotrix)
png("table_with_prop_test.png",height=200)
plot(1:5,type="n",axes=FALSE,xlab="",ylab="")
addtable2plot(1,3,table_with_prop_test(x),
  display.rownames=TRUE)
dev.off()

Jim



More information about the R-help mailing list