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?
Try this:
long <- as.data.frame.table(datax)
longer <- long[rep(1:nrow(long), long$Freq), -3]
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With