[R] Convert dataframe to table with counts where column names become row names

ghinkle ghinkle at alnylam.com
Thu Aug 6 22:50:36 CEST 2009


Wow. Very nice.  Amazingly terse.   I will throw out the looping function I
just debugged...

greg


Henrique Dallazuanna wrote:
> 
> Try this:
> t(sapply(DF1, table))
> 
> On Thu, Aug 6, 2009 at 2:14 PM, ghinkle <ghinkle at 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 at 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]]
> 
> 
> ______________________________________________
> R-help at 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.
> 
> 

-- 
View this message in context: http://www.nabble.com/Convert-dataframe-to-table-with-counts-where-column-names-become-row-names-tp24850797p24854158.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list