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 to a file and keep it beautifully formatted and aligned 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 correctly formatted in the R environment, I thought it should be trivial to write to a file with the same format. Apparently, I was wrong. I tried playing with format() and write.matrix from the MASS library, but it does not give the desired result.
I saw several sentences, such as this one , but it seems too complicated and, more importantly, does not give the desired result when printing to a file with write.table() .
So, how can I print my data frame in a text file and look the same as in 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)
outputs the following result:
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β have shifted to the right to fit their respective columns?
Since this might be relevant now, here's how to create a data.frame file:
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)