Tables extraction in R ?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Tables extraction in R ?

Greeknovice
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?
Thanks,
S.G.Golf.
Reply | Threaded
Open this post in threaded view
|

Re: Tables extraction in R ?

John Kane
Have a look at the xtables package.  I have not used it in some time but I think it may do what you want.  A google search "R statistics xtables" should bring up some useful information on this.

John Kane
Kingston ON Canada


> -----Original Message-----
> From: [hidden email]
> Sent: Fri, 6 Jul 2012 10:23:13 -0700 (PDT)
> To: [hidden email]
> Subject: [R] Tables extraction in R ?
>
> 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?
> Thanks,
> S.G.Golf.
>
> --
> View this message in context:
> http://r.789695.n4.nabble.com/Tables-extraction-in-R-tp4635638.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

____________________________________________________________
Receive Notifications of Incoming Messages
Easily monitor multiple email accounts & access them with a click.
Visit http://www.inbox.com/notifier and check it out!

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Tables extraction in R ?

David Carlson
You could use xtable (no "s"). It produces latex and html formats, but for
Excel you want to use html format.

> library(xtable)
> agec <- sample(c(rep(1, 15), rep(2, 4), 3), 1000, replace=TRUE)
> table(agec)
> agec
  1   2   3
759 202  39
> print(xtable(table(agec)), type="html", file="clipboard-128")

The print command copies the html formatted table into the Windows
Clipboard. Now just open an Excel spreadsheet and paste the table into it.

Or change "clipboard-128" to "mytable.html" and import the table.

But you also asked about making a graphics version. For that you probably
want addtable2plot() in the plotrix package. For example:

> library(plotrix)
> tbl <- table(agec)
> tbldf <- as.data.frame.table(tbl)
> barplot(tbl)
> addtable2plot(2, 500, tbldf)

Or if you just want the table:

> plot(0:3, 0:3, xlab="", ylab="", type="n", axes=FALSE)
> addtable2plot(1, 1, tbldf, cex=2)

----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352




> -----Original Message-----
> From: [hidden email] [mailto:r-help-bounces@r-
> project.org] On Behalf Of John Kane
> Sent: Friday, July 06, 2012 12:55 PM
> To: Greeknovice; [hidden email]
> Subject: Re: [R] Tables extraction in R ?
>
> Have a look at the xtables package.  I have not used it in some time
> but I think it may do what you want.  A google search "R statistics
> xtables" should bring up some useful information on this.
>
> John Kane
> Kingston ON Canada
>
>
> > -----Original Message-----
> > From: [hidden email]
> > Sent: Fri, 6 Jul 2012 10:23:13 -0700 (PDT)
> > To: [hidden email]
> > Subject: [R] Tables extraction in R ?
> >
> > 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?
> > Thanks,
> > S.G.Golf.
> >
> > --
> > View this message in context:
> > http://r.789695.n4.nabble.com/Tables-extraction-in-R-tp4635638.html
> > Sent from the R help mailing list archive at Nabble.com.
> >
> > ______________________________________________
> > [hidden email] mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> > http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
>
> ____________________________________________________________
> Receive Notifications of Incoming Messages
> Easily monitor multiple email accounts & access them with a click.
> Visit http://www.inbox.com/notifier and check it out!
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-
> guide.html
> and provide commented, minimal, self-contained, reproducible code.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Tables extraction in R ?

Jim Lemon
In reply to this post by Greeknovice
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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: Tables extraction in R ?

Greeknovice
I would like to thank all of you for spending your precious time in ordrer to help me out.
I really appreciate the fact that experienced R users replied to my  "newbie" post.
Regards,
Spiros Gkolfinopoulos