#I am having a lot of trouble reshaping this data. #This is just an examination of sample size on the margin of error that I did for a colleague. #Nothing complicated. #But restructuring the data...another story
#Here's code to produce the dataset: n <- seq(10000, 100000, by=10000) P <- seq(0.1, 0.5, by=0.1) Meff <- seq(3, 8, by=1) # 4 age groups by 2 sex categories - these are population distribution sizes - order is males 18-29, females 18-29, males 30-44,females 30-44, males 45-59, females 45-59, males 60+, females 60+ Age1 <- data.frame(matrix(c(0.196598808, 0.224390935, 0.149289474, 0.151446387,0.08750253, 0.076399683, 0.060799283, 0.053572898), nrow=4, ncol=2, byrow=T, dimnames=list(seq(1,4, by=1), c("Males", "Females")))) MOE <- NULL for (i in n){ for (p in P){ for (m in Meff){ for (a in Age1[,1]){ ni <- i * a M <- sqrt((1.92^2)*p*(1-p)*m/ni) dta <- data.frame(a, i, ni, p, m, M) colnames(dta) <- c("Proportion", "Total_n", "Stratum_n", "P", "Meff", "MOE") dta$Sex <- "Males" MOE <- rbind(MOE, dta) } for (a in Age1[,2]){ ni <- i * a M <- sqrt((1.92^2)*p*(1-p)*m/ni) dta <- data.frame(a, i, ni, p, m, M) colnames(dta) <- c("Proportion", "Total_n", "Stratum_n", "P", "Meff", "MOE") dta$Sex <- "Females" MOE <- rbind(MOE, dta) } } } } # What I want is data that looks like: # Meff Proportion Sample Size Males_0.1 Females_0.1 Males_0.2 Females_0.2 ..... Males_0.5 Females_0.5 #I'm stumped on how to reshape this. #I tried this but it gave me the attached output (yuck): library(reshape2) library(plyr) tmp <- ddply(MOE, .(Meff), mutate, id=paste(Meff, Sex, seq_along(MOE))) wd <- dcast(tmp, id + Meff ~ P + Sex, value.var="MOE") ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see 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.