打印列对齐的数据框(如R所示) [英] Print a data frame with columns aligned (as displayed in R)

查看:390
本文介绍了打印列对齐的数据框(如R所示)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在R中有以下数据框:

 > dframe 
平均中位数
候选人85.68 60
非公币9.21 4
多27.48 17
单声道4.43 3
多不公牌22.23 15

我想将其打印成一个文件,并保持格式良好并对齐,如上所示。我使用:

  write.table(dframe,file =test,sep =\t,quote = F )

产生以下输出:

 平均中位数
候选人85.68 60
NonCands 9.21 4
多27.48 17
单声道4.43 3
多人非法车22.23 15

由于数据在R环境中显示格式正确,我认为将其写入文件应该是微不足道的具有相同的格式。显然我错了我尝试使用MASS库中的 format() write.matrix ,但都不会产生所需的结果。



我已经看到一些建议,如这一个,但它似乎太复杂了,更重要的是,在使用 write.table()打印到文件时不会产生所需的结果。



那么,如何将数据框打印到一个文本文件中,并且看起来就像在R中一样?






更新



按照Justin从他的评论中的建议,我安装了gdata库,并使用了 write.fwf 。这是几乎我需要的:

  write.fwf(dframe,file =test sep =\t,quote = F,rownames = T)

产生以下输出:

 平均中位数
候选人85.68 60
NonCands 9.21 4
多27.48 17
Mono 4.43 3
多不可取22.23 15

所以,任何关于如何获得平均值和中位数向右移动,以便它们与各自的列对齐。



由于现在可能是相关的,以下是data.frame的创建方式:

  labels< -c(Candidates,NonCands,Multi,Mono,Multi NonCands 
平均值(平均值(cands),平均值(非),平均值(多),平均值(单数),平均值(多重))
中值中位数(多),中值(单),中位数(multi_non))
名称(平均值)< -labels
dframe< -data.frame(Mean,Median)


解决方案

您可以将 print 的输出重定向到文件。

  max.print<  -  getOption('max.print')
options(max.print = nrow(dframe)* ncol(dframe))
sink('dframe.txt')
dframe
sink()
options(max.print = max.print)


I have the following data frame in R:

> dframe
                Mean Median
Candidates     85.68     60
NonCands        9.21      4
Multi          27.48     17
Mono            4.43      3
Multi NonCands 22.23     15

I want to print it into a file and keep it nicely formatted and aligned just as shown above. I use:

write.table(dframe,file="test",sep="\t", quote=F)

which produces the following output:

Mean    Median
Candidates  85.68   60
NonCands    9.21    4
Multi   27.48   17
Mono    4.43    3
Multi NonCands  22.23   15

Since the data is displayed properly formatted within the R environment I thought it should be trivial to write it to a file with the same format. Apparently I was wrong. I have tried playing with format() and write.matrix from the MASS library but neither produces the desired result.

I have seen some suggestions like this one, but it seems both too complicated and, more importantly, does not produce the desired result when printing to a file with write.table().

So, how can I print my data frame to a text file and have it look just as it does within R?


UPDATE

Following Justin's suggestion from his comment below, I installed the gdata library and used write.fwf. This is almost what I need:

write.fwf(dframe,file="test",sep="\t", quote=F, rownames=T)

produces the following output:

Mean    Median
Candidates      85.68   60
NonCands         9.21    4
Multi           27.48   17
Mono             4.43    3
Multi NonCands  22.23   15

So, any ideas on how to get "Mean" and "Median" shifted to the right so they align with their respective columns?

Since it may now be relevant, here is how the data.frame was created:

labels<-c("Candidates","NonCands","Multi", "Mono", "Multi NonCands")
Mean <- c(mean(cands), mean(non),mean(multi),mean(mono),mean(multi_non))
Median <- c(median(cands), median(non),median(multi),median(mono),median(multi_non))
names(Mean)<-labels
dframe<-data.frame(Mean,Median)

解决方案

You could redirect the output of print to file.

max.print <- getOption('max.print')
options(max.print=nrow(dframe) * ncol(dframe))
sink('dframe.txt')
dframe
sink()
options(max.print=max.print)

这篇关于打印列对齐的数据框(如R所示)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆