Hello R gurus, I have a data set from which i have to extract the gender and age matched rows from controls and disease group disease<-paste(rep(c('y','n'),11)) gender<-paste(rep(c('m','f'),11)) mcp<-rnorm(700,1400) age<-rnorm(32,34) dat<-data.frame(disease=disease,sex=gender,Dr_age=age[1:22],MCP=mcp[1:22]) I have other categorical variables also to add to the matching. all the posts I came across are matching for a single column from two matrix/dataframe. How can i match for multiple variables when all the data is in one single dataframe -- View this message in context: http://r.789695.n4.nabble.com/matching-by-gender-and-age-tp3552825p3552825.html Sent from the R help mailing list archive at Nabble.com.
On May 26, 2011, at 11:19 AM, 1Rnwb wrote:> Hello R gurus, I have a data set from which i have to extract the > gender and > age matched rows from controls and disease groupYou need to define what you mean by "age-matched". Your example creates a very narrow age range which further adds questions about what you are doing. You probably need to look at defining an age- category variable with cut() and proceeding from there.> disease<-paste(rep(c('y','n'),11)) > gender<-paste(rep(c('m','f'),11)) > mcp<-rnorm(700,1400) > age<-rnorm(32,34) > > dat<- > data.frame(disease=disease,sex=gender,Dr_age=age[1:22],MCP=mcp[1:22])Use set.seed to generate a reproducible input and then tell us what your expected output should be.> > I have other categorical variables also to add to the matching. all > the > posts I came across are matching for a single column from two > matrix/dataframe. How can i match for multiple variables when all > the data > is in one single dataframe >David Winsemius, MD West Hartford, CT
On Jun 3, 2011, at 1:37 PM, 1Rnwb wrote:> Thanks, for pointing out the package e1071, the example for > matchControls is > exactly what I am looking for, however how can I add additional > factors to > match for. > library(e1071) > Age.case <- 40 + 5 * rnorm(50) > Age.cont <- 45 + 10 * rnorm(150) > Age <- c(Age.case, Age.cont) > > Sex.case <- sample(c("M", "F"), 50, prob = c(.4, .6), replace = TRUE) > Sex.cont <- sample(c("M", "F"), 150, prob = c(.6, .4), replace = TRUE) > Sex <- as.factor(c(Sex.case, Sex.cont)) > > casecont <- as.factor(c(rep("case", 50), rep("cont", 150))) > risk.case<-sample(c("H", "M", "L"), 50, prob = c(.4,.4, .2), replace > = TRUE) > risk.cont<-sample(c("H", "M", "L"), 150, prob = c(.3,.3, .4), > replace > TRUE) > risk<-as.factor(c(risk.case, risk.cont)) > > > ## now look at the group properties: > boxplot(Age ~ casecont) > barplot(table(Sex, casecont), beside = TRUE) > barplot(table(Sex, risk), beside = TRUE) > > m <- matchControls(casecont ~ Sex + Age+risk)It runs on my device. What is the question? Are you getting an error? -- David Winsemius, MD West Hartford, CT