How to bring ftable to excel in R without a lot of formatting (merge, unquote)

微笑、不失礼 提交于 2019-12-23 03:16:00

问题


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)


来源:https://stackoverflow.com/questions/28638373/how-to-bring-ftable-to-excel-in-r-without-a-lot-of-formatting-merge-unquote

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!