[R] Sweave function

Allan Engelhardt allane at cybaea.com
Thu Jul 1 11:05:54 CEST 2010


In general, use cat (see help("cat")) for printing your output, e.g.

<<echo=FALSE>>=
cat(levels(mydata$Firms)[mydata$Firms], "\n")
cat(mydata$Year, "\n")
# etc.
@


For your specific case you may also be interested in help("xtable", 
package="xtable") along with the <<...,results=tex>>= Sweave construct.

For an R version of the output you want, a starting point may be

for (f in levels(mydata$Firms)) {
      cat(f, "\n")
      print(t(mydata[mydata$Firms == f, 2:5]))
}


which gives

electrolux
         1    2    3
Year 1995 1996 1997
IIA   100  340   35
IIB    45   67   99
IIC    65   97   31
fiat
         4    5    6
Year 1995 1996 1997
IIA    76  567  453
IIB    34   35   89
IIC    45   66   37


Hope this helps a little.

Allan


On 01/07/10 09:08, n.vialma at libero.it wrote:
> Dear list,
> I have a question about the interaction between R code and Latex language trough the Sweave function in the package "utils".
> What I'm  trying to do is to write a report.  Contrary to the examples shown in the Sweave Manual in which table already constructed by R are "exported" on Latex files, what I would like to do is to build a table in which I combine text and specific columns of my data frame. I will give you the following example to be much more clear.
> Suppose I have a data frame like this:
>
> Firms                        Year                 IIA                       IIB          IIC
> electrolux                1995                100                       45            65
> electrolux                1996                340                      67            97
> electrolux                1997                35                         99           31
> fiat                            1995                 76                        34          45
> fiat                            1996                567                       35          66
> fiat                            1997                453                      89          37
>
>
> Where IIA is the turnover of the firm, IIB is the production and IIC is the cost of labour.
>
> I would like to get a table like this in the latex format this:
> Firms 1
> electrolux
> -----------------------------------------------------------------------------------
>
>    variables                     1995         1997        1997
>
> ----------------------------------------------------------------------------------
> turnover                         100          340            35
> production                      45            67              99
> cost of labour                  65            97             31
> --------------------------------------------------------------------------------
>
>
> I use the following code:
> \documentclass[a4paper]{article}
> \title{example}
> \begin{document}
>
> \maketitle
> <<echo=F>>=
> mydata$firms
> @
> variables
> <<echo=F>>=
> mydata$year
> @
>
>
> and so on. I have two problem, first I'm not able to put on the same line text and output of R. So  on my Latex document I get for example
> variable
> 1995 1996 1997
>
>
> and I don't want this. The secondo problem is that at the beginnig of the R output I get the index, namely
> variable
> [1] 1995 1996 1997
> and I don't want to see it.
> Anyone Knows how to do it or if there is another package in R that give me the possibility to create a Report by constructing table without any problems???
>
>



More information about the R-help mailing list