How to bring ftable to excel in R without a lot of

2019-06-08 15:15发布

问题:

What I miss about Pandas(Python) working on R is the way a DataFrame is exported to excel. In R, ftable shows tables similar as pandas multiindex DataFrame. When I use to_excel in pandas every row and column is ok in every cell even merge cells in column names if necesary.

In R I tried write.ftable with write.table

df = data.frame(a = c(1:6), b = rep(c('G1','G2'),3), c = rep(c('A','D','F'),2), d = c('F','F','F','F','M','M'))
df2 = ftable(xtabs(a~b+c+d, df), row.vars = 1)
write.table(write.ftable(df2))

But I need spend a lot of time formatting (text to column, unquoting, merging, etc) in excel.

There is a way (package) in R to export ftable to excel without to make a lot of formatting things. Thanks in advance.

回答1:

Try this:

library(xtable)
print(xtable(as.matrix(df2)), type = "html", file = "out.html")
file.show("out.html")

Now copy the output and paste it into Excel.



回答2:

How about:

library(DescTools)
txt <- stats:::format.ftable(df2, quote=FALSE)
XLView(txt, row.names = FALSE, col.names = FALSE)