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???
[[alternative HTML version deleted]]
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???
>
>
Dear list,
I'm trying to generate a latex Document in which there are a lot of tables.
I'm using the Sweave function in the package utils, but I'm having a lot
of problems with the format. This is my code:
\documentclass[a4paper]{amsbook}
\title{Schema di bilancio}
\begin{document}
\maketitle
<<echo=F,results=hide>>report=Bilanci
mynames<-names(report)
mynames[mynames=="AA01"]<-"Immobilizzazioni tecniche
nette"
mynames[mynames=="AA01I"]<-"Immobilizzazioni imm. nette"
mynames[mynames=="AA01M"]<-"Immobilizzazioni mat. nette"
mynames[mynames=="AA02"]<-"Partecipazioni e crediti fin."
mynames[mynames=="AA02B"]<-"Attivita' fin. a breve"
mynames[mynames=="AA02L"]<-"Immobilizzazioni finan."
mynames[mynames=="AA03"]<-"Magazzino"
mynames[mynames=="AA04"]<-"Crediti commerciali"
mynames[mynames=="AA05"]<-"Liquidita'"
mynames[mynames=="AA06"]<-"Altre attivita'"
mynames[mynames=="AA07"]<-"Tot attivita'"
mynames[mynames=="AL01"]<-"Capitale netto"
mynames[mynames=="AL02"]<-"Fondo tfr"
mynames[mynames=="AL03"]<-"Altri fondi"
mynames[mynames=="AL04"]<-"Debiti commerciali"
mynames[mynames=="AL04A"]<-"Anticipi di clienti"
mynames[mynames=="AL04B"]<-"Debiti vs fornitori"
mynames[mynames=="AL05"]<-"Debiti fin. tot."
mynames[mynames=="AL05B"]<-"Debiti fin. a breve"
mynames[mynames=="AL05L"]<-"Debiti fin. a medio/lungo"
mynames[mynames=="AL99"]<-"Altre passivita'"
mynames[mynames=="AL06"]<-"Tot passivita'"
mynames[mynames=="EC01"]<-"Ricavi netti"
mynames[mynames=="EC02"]<-"Produzione int. capitalizzate"
mynames[mynames=="EC03"]="Variazione scorte prod finiti"
mynames[mynames=="EC04"]<-"Acquisti"
mynames[mynames=="EC05"]<-"Variazioni scorte mat. prime"
mynames[mynames=="EC06"]<-"Costi per servizi e god. di beni
terzi"
mynames[mynames=="EC07"]<-"Costo del lavoro tot"
mynames[mynames=="EC08"]<-"Ammortamenti e accantonamenti"
mynames[mynames=="EC08A"]<-"Ammortamenti"
mynames[mynames=="EC08B"]<-"Accantonamenti e utilizzi di
riserve"
mynames[mynames=="EC09"]<-"Oneri fin."
mynames[mynames=="EC10"]<-"Proventi fin."
mynames[mynames=="EC11"]<-"Ricavi diversi netti"
mynames[mynames=="EC11A"]<-"Altri ricavi netti ord"
mynames[mynames=="EC11C"]<-"Contributi in conto
esercizio"
mynames[mynames=="EC12"]<-"Proventi straord. netti"
mynames[mynames=="EC13"]<-"Imposte"
mynames[mynames=="EC14"]<-"Utile netto rettificato"
mynames[mynames=="EC15"]<-"Rettifiche"
mynames[mynames=="EC16"]<-"Utile dell'esercizio"
names(report)<-mynames
report=split(report,report$CFISCALE)
report1=lapply(report,function(x){
t(x)})
@
<<echo=F,results=tex>>report2=lapply(report1, function(x) {
print(xtable(x,na.string="-"))})
@
\end{document}
Even if I put the code referring to the title, in my pdf document I don't
get it and I don't know why. Secondly I get the following error
message:(\end occurred when \ifnum on line 2150 was incomplete)
the results of this error is that I loose a lot of tables. Instead of having 500
tables I have just 250 tables. Another problem is that the format is not what I
would like to get, my tables appear at the center of the page and I would like
to put them at the left (the result is that my table are cut), and I don't
know how to do that, I've tried to put in the xtable function the option
table.placement="H" but it seems that it doesn't work.An example
of what I get by using the split function and then the xtable function is:
& 49 & 48 & 47 \\
\hline
CFISCALE & 5060157 & 5060157 & 5060157 \\
RAGSOCB & GIUSEPPE TARENZI S.R.L. & GIUSEPPE TARENZI S.R.L.
& GIUSEPPE TARENZI S.R.L. \\
ANNO & 2005 & 2006 & 2007 \\
Ricavi netti & 77 & 98 & 124 \\
Produzione int. capitalizzate & 0 & 0 & 0 \\
Variazione scorte prod finiti & 2059 & 2105 & 2120 \\
Acquisti & 1542 & 1564 & 1576 \\
Costi per servizi e god. di beni terzi & 122 & 135 &
121 \\
Costo del lavoro tot & 273 & 281 & 301 \\
Ammortamenti e accantonamenti & 11 & 5 & 7 \\
Ammortamenti & 9.9 & 4.5 & 6.3 \\
Accantonamenti e utilizzi di riserve & 1.1 & 0.5 & 0.7
\\
Oneri fin. & 38 & 42 & 35 \\
Proventi fin. & 1 & 0 & 1 \\
Ricavi diversi netti & 0 & -13 & -33 \\
Altri ricavi netti ord & 0 & -13 & -33 \\
Contributi in conto esercizio & 0 & 0 & 0 \\
Proventi straord. netti & 1 & 0 & 0 \\
Imposte & 73 & 78 & 80 \\
Utile netto rettificato & 79 & 85 & 92 \\
Utile dell'esercizio & 79 & 85 & 92 \\
Immobilizzazioni tecniche nette & 269 & 267 & 274 \\
Partecipazioni e crediti fin. & 0 & 3 & 0 \\
Magazzino & 592 & 623 & 656 \\
Crediti commerciali & 17.56393 & 28.15887 & 26.14891
\\
Liquidita' & 14 & & 177 \\
Altre attivita' & -892.564 & & -1133.149 \\
Tot attivita' & 0.000000000 & 0.005924951 &
0.000000000 \\
Immobilizzazioni imm. nette & 53.8 & 53.4 & 54.8 \\
Immobilizzazioni mat. nette & 215.2 & 213.6 & 219.2 \\
Attivita' fin. a breve & 0.0 & 1.5 & 0.0 \\
Immobilizzazioni finan. & 0.0 & 1.5 & 0.0 \\
Capitale netto & 0.000000000 & 0.002773298 &
0.000000000 \\
Fondo tfr & -558.4306 & -666.3115 & -533.6478 \\
Altri fondi & 0 & 0 & 0 \\
Debiti commerciali & 558.4306 & 666.3146 & 533.6478 \\
Debiti fin. tot. & 0.000000e+00 & 2.340336e-05 &
0.000000e+00 \\
Tot passivita' & 0.000000000 & 0.005924951 &
0.000000000 \\
Anticipi di clienti & 0 & 0 & 0 \\
Debiti vs fornitori & 558.4306 & 666.3146 & 533.6478
\\
Debiti fin. a breve & 0.000000e+00 & 1.170168e-05 &
0.000000e+00 \\
Debiti fin. a medio/lungo & 0.000000e+00 & 1.170168e-05
& 0.000000e+00 \\
Altre passivita' & 0 & 0 & 0 \\
I would like to not see the first line, namely & 49 & 48
& 47 \\, in my table wich is the result of the split function applied to
a list. Secondly I don't want the repetion of the variables Cfiscale and
Ragsocb, so my result should be:
\hline
CFISCALE & 5060157 \\
RAGSOCB & GIUSEPPE TARENZI S.R.L.\\
but I don't know I to solve it in a list.
Someone knows how to solve these problems!
Thanks for your attention!
[[alternative HTML version deleted]]