ghinkle
2009-Aug-06 17:14 UTC
[R] Convert dataframe to table with counts where column names become row names
Can anyone explain how best to go from a dataframe to a table (or better yet a new dataframe) of counts, where the row names in the new table (or dataframe) are the column names of the original df. start w/ DF1 = Pos1 Pos2 Pos3 .... oligo1 G C A oligo2 U U A oligo3 G C C oligo4 C G U oligo5 A A G ..... End with DF2 = G A U C Pos1 2 1 1 1 Pos2 1 1 1 2 Pos3 1 2 1 1 .... I know how to generate the counts of each one column at a time using "table(DF1$Pos1)". Is there a way to do this in one step? Should I just write a for loop for each of the columns? thanks, greg -- View this message in context: http://www.nabble.com/Convert-dataframe-to-table-with-counts-where-column-names-become-row-names-tp24850797p24850797.html Sent from the R help mailing list archive at Nabble.com.
Henrique Dallazuanna
2009-Aug-06 20:24 UTC
[R] Convert dataframe to table with counts where column names become row names
Try this: t(sapply(DF1, table)) On Thu, Aug 6, 2009 at 2:14 PM, ghinkle <ghinkle@alnylam.com> wrote:> > Can anyone explain how best to go from a dataframe to a table (or better > yet > a new dataframe) of counts, where the row names in the new table (or > dataframe) are the column names of the original df. > > start w/ > DF1 > Pos1 Pos2 Pos3 .... > oligo1 G C A > oligo2 U U A > oligo3 G C C > oligo4 C G U > oligo5 A A G > ..... > > End with > > DF2 > G A U C > Pos1 2 1 1 1 > Pos2 1 1 1 2 > Pos3 1 2 1 1 > .... > > I know how to generate the counts of each one column at a time using > "table(DF1$Pos1)". > Is there a way to do this in one step? Should I just write a for loop for > each of the columns? > > thanks, > > greg > -- > View this message in context: > http://www.nabble.com/Convert-dataframe-to-table-with-counts-where-column-names-become-row-names-tp24850797p24850797.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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]]
David Winsemius
2009-Aug-06 20:39 UTC
[R] Convert dataframe to table with counts where column names become row names
On Aug 6, 2009, at 1:14 PM, ghinkle wrote:> > Can anyone explain how best to go from a dataframe to a table (or > better yet > a new dataframe) of counts, where the row names in the new table (or > dataframe) are the column names of the original df. > > start w/ > DF1 > Pos1 Pos2 Pos3 .... > oligo1 G C A > oligo2 U U A > oligo3 G C C > oligo4 C G U > oligo5 A A G > .....> apply(DF1, 2, table) Pos1 Pos2 Pos3 A 1 1 2 C 1 2 1 G 2 1 1 U 1 1 1 Since tables are really matrices, the t() operation would bring you to your goal: > t( apply(DF1, 2, table) ) A C G U Pos1 1 1 2 1 Pos2 1 2 1 1 Pos3 2 1 1 1> > End with > > DF2 > G A U C > Pos1 2 1 1 1 > Pos2 1 1 1 2 > Pos3 1 2 1 1 > .... > > I know how to generate the counts of each one column at a time using > "table(DF1$Pos1)". > Is there a way to do this in one step? Should I just write a for > loop for > each of the columns?--- David Winsemius, MD Heritage Laboratories West Hartford, CT