Dear Everyone, I try to create a cvs-file with different results form the table function. Imagine a data-frame with two vectors a and b where b is of the class factor. I use the tapply function to count a for the different values of b. tapply(a,b,table) and I use the table function to have a look of the frequencies as a total table(a) I would like to put both results together in one txt or csv file that I can import to e.g. Excel. The export file should have a layout like 1,2,3,4,5,6,7 (possible values of a) 3,6,7,8,8,8,1 (Counts of a total) 1,2,3,4,5,3,0 (Counts of a where b==A) 2,4,4,4,3,5,1 (Counts of a where b==B) I tried to change the class of the table result to a matrix but I could not find a way to use the results of tapply. I use tapply because b has 15 different values. Thanx Andreas Kunzler ____________________________ Bundeszahn?rztekammer (BZ?K) Chausseestra?e 13 10115 Berlin Tel.: 030 40005-113 Fax: 030 40005-119 E-Mail: a.kunzler at bzaek.de
Try creating a new object: tb <- rbind(table(a), do.call(rbind.data.frame, tapply(a, b, table))) names(tb) <- unique(a) then write to csv by write.table. On Tue, Sep 9, 2008 at 5:48 AM, Kunzler, Andreas <a.kunzler@bzaek.de> wrote:> Dear Everyone, > > I try to create a cvs-file with different results form the table function. > > Imagine a data-frame with two vectors a and b where b is of the class > factor. > > I use the tapply function to count a for the different values of b. > > tapply(a,b,table) > > and I use the table function to have a look of the frequencies as a total > > table(a) > > I would like to put both results together in one txt or csv file that I can > import to e.g. Excel. > > The export file should have a layout like > > 1,2,3,4,5,6,7 (possible values of a) > 3,6,7,8,8,8,1 (Counts of a total) > 1,2,3,4,5,3,0 (Counts of a where b==A) > 2,4,4,4,3,5,1 (Counts of a where b==B) > > I tried to change the class of the table result to a matrix but I could not > find a way to use the results of tapply. I use tapply because b has 15 > different values. > > Thanx > > Andreas Kunzler > ____________________________ > Bundeszahnärztekammer (BZÄK) > Chausseestraße 13 > 10115 Berlin > > Tel.: 030 40005-113 > Fax: 030 40005-119 > > E-Mail: a.kunzler@bzaek.de > > ______________________________________________ > R-help@r-project.org mailing list > 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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
On Tue, Sep 9, 2008 at 3:48 AM, Kunzler, Andreas <a.kunzler at bzaek.de> wrote:> Dear Everyone, > > I try to create a cvs-file with different results form the table function. > > Imagine a data-frame with two vectors a and b where b is of the class factor. > > I use the tapply function to count a for the different values of b. > > tapply(a,b,table) > > and I use the table function to have a look of the frequencies as a total > > table(a) > > I would like to put both results together in one txt or csv file that I can import to e.g. Excel. > > The export file should have a layout like > > 1,2,3,4,5,6,7 (possible values of a) > 3,6,7,8,8,8,1 (Counts of a total) > 1,2,3,4,5,3,0 (Counts of a where b==A) > 2,4,4,4,3,5,1 (Counts of a where b==B) > > I tried to change the class of the table result to a matrix but I could not find a way to use the results of tapply. I use tapply because b has 15 different values.An alternative would be to use reshape (http://had.co.nz/reshape): mydf <- data.frame( a = sample(7, 100, rep = T), b sample(letters[1:15], 100, rep = T)) library(reshape) mydf$value <- 1 cast(mydf, b ~ a, sum, margins="row.major", fill = 0) Regards, Hadley -- http://had.co.nz/