writing data frame to pdf table writing data frame to pdf table r r

writing data frame to pdf table


This code should work:

library(gridExtra)df <- read.table(text = "1/1/2012  USA     51/1/2012  Japan   41/2/2012  USA     101/3/2012  Germany 15")names(df) <- c("Date","Country","Trade")EqDatedf <- as.data.frame(df[1,])EmptyLine <- data.frame(Date = "",Country = "",Trade = "")pdf(file = "q.pdf")for (i in 2:nrow(df)) {if (as.vector(df$Date[i])  ==  as.vector(df$Date[i-1])) {EqDatedf <- rbind(EqDatedf, df[i,])}else {EqDatedf <- rbind(EqDatedf, EmptyLine)EqDatedf <- rbind(EqDatedf, df[i,])      }}grid.table(EqDatedf, show.rownames = FALSE)dev.off()

enter image description here


I really recommend you to use Rstudio with Knitr. It is very easy to create good reports.

For example,

\documentclass{article}\begin{document}<<myTable,results='asis'>>=library(xtable)tab <- read.table(text = 'Date    County    Trade1/1/2012  USA     51/1/2012  Japan   41/2/2012  USA     101/3/2012  Germany 15',header = TRUE)print(xtable(tab),hline.after=c(2,3))   ## print.xtable have many smart options@\end{document}

enter image description here


As of 2017, there is good support in R-studio presentation formats (Markdown) with package "pander", and output to PDF via Beamer.See pander : http://rapporter.github.io/pander/#pander-an-r-pandoc-writer

Example in R-studio presentation code to print a data frame as table :

```{r}    pander(df)```