Farrel Buchinsky
2007-Oct-02 15:19 UTC
[R] Calculating proportions from a data frame rather than a table
When one has raw data it is easy to create a table of one variable against another and then calculate proportions For example a.nice.table<-table(a,b) prop.table(a.nice.table,1) However, I looked at several papers and created a data frame of the aggregate data. That means I acually created a table except it is a data frame. The first column lists the name of the first author and the year. I cannot find how to convert the data frame to a table so I can use great functions such as prop.table and margin.table. Alas I tried, rowSums but it provides lousy output without listing the names of th papers. I have thought of going through the reshape package but I suspect that there is an easier way to convert a data frame to a table. Is there? -- Farrel Buchinsky [[alternative HTML version deleted]]
John Kane
2007-Oct-02 20:48 UTC
[R] Calculating proportions from a data frame rather than a table
What am I missing here? Cannot you just create the table from the data.frame and apply prop.table()to it? --- Farrel Buchinsky <fjbuch at gmail.com> wrote:> When one has raw data it is easy to create a table > of one variable against > another and then calculate proportions > For example > a.nice.table<-table(a,b) > prop.table(a.nice.table,1) > > However, I looked at several papers and created a > data frame of the > aggregate data. That means I acually created a table > except it is a data > frame. The first column lists the name of the first > author and the year. > I cannot find how to convert the data frame to a > table so I can use great > functions such as prop.table and margin.table. > > Alas I tried, rowSums but it provides lousy output > without listing the names > of th papers. > I have thought of going through the reshape package > but I suspect that there > is an easier way to convert a data frame to a table. > Is there? > > -- > Farrel Buchinsky
Farrel Buchinsky
2007-Oct-03 23:28 UTC
[R] Calculating proportions from a data frame rather than a table
Incidentally, the feature becomes really powerful when one uses functions such as addmargins together with prop.table as in coinf.table <-as.matrix(coinfection) prop.table(coinf.table,1) # to see proportions from each HPV type per study addmargins(coinf.table)# to see totals within study and accross all studies prop.table(addmargins(coinf.table,1),1)# to see proportions for all studies added together On 10/3/07, Farrel Buchinsky <fjbuch at gmail.com> wrote:> Genius! Thank you very much. > Yes indeed I should have thought of that. For some reason I have a > mental blank about the use of row.names and instead I repeatedly put > that kind of data as a column in the data frame. Thank you for the > rescue. >-- Farrel Buchinsky GrandCentral Tel: (412) 567-7870