Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate data.frame from frequency table

Tags:

r

I have synthetic data in a 2*4 array with 500 observations:

datax = array(c(120, 181, 50, 43, 41, 33,24,8), dim=c(2,4))
dimnames(datax) = list(gender= c('male', 'female')
                    , punishment = c('None', 'Community_service', 'Youth_prison', 'Normal_prison'))

I'd like to produce a data.frame from the table that represents the "source" of the frequency table.

I can represent it through a "Freq" column (as.data.frame(as.table(datax)), also here) but I'd like to produce the data.frame with 500 rows and 2 columns (gender, punishment).

How would I do this in R?

like image 802
ben_aaron Avatar asked Sep 20 '25 06:09

ben_aaron


2 Answers

Try this:

long <- as.data.frame.table(datax)
longer <- long[rep(1:nrow(long), long$Freq), -3]
like image 126
G. Grothendieck Avatar answered Sep 21 '25 20:09

G. Grothendieck


Using dplyr:

as.data.frame.table(datax) %>%
    rowwise() %>%
    do(data.frame(rep(.$gender, .$Freq), .$punishment))

This constructs a new table for every row in your data, repeating Freq times, and concatenates them into one giant table.

like image 40
Konrad Rudolph Avatar answered Sep 21 '25 19:09

Konrad Rudolph