[R] How to print the frequency table (produced by the command "table" to Excel
Jim Lemon
drjimlemon at gmail.com
Sun May 1 02:06:12 CEST 2016
Hi jpm miao,
I think you can get what you want like this:
alpha1<-sample(LETTERS[1:3],50,TRUE)
alpha2<-sample(LETTERS[1:2],50,TRUE)
alphas<-data.frame(alpha1,alpha2)
library(prettyR)
alphatab<-xtab(alpha1~alpha2,alphas)
sink("temp_table3.csv",append=TRUE)
delim.xtab(alphatab,pct=NA,delim=",")
sink()
Jim
On Sun, May 1, 2016 at 4:47 AM, jpm miao <miaojpm at gmail.com> wrote:
> Jim,
>
> Thanks for creating such a fantastic package "prettyR".
> I want to print the pretty frequency table (with row total and column
> total) to an excel (or csv ) file. Is it possible?
>>alphatab
>
> A B Total
> A 8 10 18
> B 7 5 12
> C 9 11 20
> Total 24 26 50
>
> Two issues I encountered (See the attached csv file).
> 1. When I tried to print the above table to csv file, all elements on the
> same row are printed in one cell.
> 2. If I write "delim.table(alpha tab)", the table is distorted (see
> attached). Of course, I can adjust it manually but sometimes the number of
> files is big.
>
> Thanks!
>
> Miao
>
>> alpha1<-sample(LETTERS[1:3],50,TRUE)
>> alpha2<-sample(LETTERS[1:2],50,TRUE)
>>
>> alphas<-data.frame(alpha1,alpha2)
>> alphatab<-xtab(alpha1~alpha2,alphas)
> Crosstabulation of alpha1 by alpha2
> alpha2
> alpha1 A B
> A 8 10 18
> 44.44 55.56 -
> 33.33 38.46 36.00
>
> B 7 5 12
> 58.33 41.67 -
> 29.17 19.23 24.00
>
> C 9 11 20
> 45 55 -
> 37.50 42.31 40.00
>
> 24 26 50
> 48 52 100
>> delim.xtab(alphatab,pct=NA,interdigitate=TRUE)
> alphatab
>
> A B Total
> A 8 10 18
> B 7 5 12
> C 9 11 20
> Total 24 26 50
>
>> sink("temp_table3.csv")
>> delim.xtab(alphatab,pct=NA,interdigitate=TRUE)
>> sink()
>> sink("temp_table3.csv", append=TRUE)
>> delim.table(alphatab)
>> sink()
>> sink("temp_table3.csv", append=TRUE)
>> delim.table(alphatab)
>> sink()
>> ?delim.xtab
>
>
> 2016-04-26 16:14 GMT-07:00 Jim Lemon <drjimlemon at gmail.com>:
>>
>> Hi jpm miao,
>> You can get CSV files that can be imported into Excel like this:
>>
>> library(prettyR)
>> sink("excel_table1.csv")
>> delim.table(table(df[,c("y","z")]))
>> sink()
>> sink("excel_table2.csv")
>> delim.table(as.data.frame(table(df[,c("y","z")])),label="")
>> sink()
>> sink("excel_table3.csv")
>> delim.table(as.matrix(table(df[,c("y","z")])),label="")
>> sink()
>>
>> Jim
>>
>> On Wed, Apr 27, 2016 at 8:35 AM, jpm miao <miaojpm at gmail.com> wrote:
>> > Hi,
>> >
>> > How could we print the frequency table (produced by "table") to an
>> > Excel
>> > file?
>> > Is there an easy way to do so? Thanks,
>> >
>> > Miao
>> >
>> >> df <- data.frame(x = 1:3, y = 3:1, z = letters[1:3])
>> >
>> >> table(df[,c("y","z")])
>> > z
>> > y a b c
>> > 1 0 0 1
>> > 2 0 1 0
>> > 3 1 0 0
>> >> test<-table(df[,c("y","z")])
>> >> as.data.frame(test)
>> > y z Freq
>> > 1 1 a 0
>> > 2 2 a 0
>> > 3 3 a 1
>> > 4 1 b 0
>> > 5 2 b 1
>> > 6 3 b 0
>> > 7 1 c 1
>> > 8 2 c 0
>> > 9 3 c 0
>> >> as.matrix(test)
>> > z
>> > y a b c
>> > 1 0 0 1
>> > 2 0 1 0
>> > 3 1 0 0
>> >> testm<-as.matrix(test)
>> >> testm
>> > z
>> > y a b c
>> > 1 0 0 1
>> > 2 0 1 0
>> > 3 1 0 0
>> >> typeof(testm)
>> > [1] "integer"
>> >
>> > [[alternative HTML version deleted]]
>> >
>> > ______________________________________________
>> > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> > https://stat.ethz.ch/mailman/listinfo/r-help
>> > PLEASE do read the posting guide
>> > http://www.R-project.org/posting-guide.html
>> > and provide commented, minimal, self-contained, reproducible code.
>
>
More information about the R-help
mailing list