Skip to content

Instantly share code, notes, and snippets.

@geofferyzh
Created May 23, 2012 18:09
Show Gist options
  • Save geofferyzh/2776754 to your computer and use it in GitHub Desktop.
Save geofferyzh/2776754 to your computer and use it in GitHub Desktop.
RinAction - Converting Table to Flat file
######################################################
# converting a table into a flat file via table2flat
######################################################
table2flat <- function(mytable) {
df <- as.data.frame(mytable)
rows <- dim(df)[1]
cols <- dim(df)[2]
x <- NULL
for (i in 1:rows) {
for (j in 1:df$Freq[i]) {
row <- df[i, c(1:(cols - 1))]
x <- rbind(x, row)
}
}
row.names(x) <- c(1:dim(x)[1])
return(x)
}
table2flat(mytable)
#########################################
# Using table2flat with published data
#########################################
treatment <- rep(c("Placebo", "Treated"), 3)
improved <- rep(c("None", "Some", "Marked"), each = 2)
Freq <- c(29, 13, 7, 7, 7, 21)
mytable <- as.data.frame(cbind(treatment, improved, Freq))
mydata <- table2flat(mytable)
head(mydata)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment