R: expand.grid と For 文
ソースコードの記載
#---------------------------------------
# For
#---------------------------------------
dum <-c()
i<-1
for (sex in c("M","F")) {
for (age in seq(30,50,by=10)) {
for (trt in c("T","P")) {
dum <- rbind(dum, t(c(sex,age,trt)))
i <- i+ 1
}
}
}
dum < as.data.frame(dum)
names(dum) <- c("sex","age","trt")
#---------------------------------------
# Expand.grid
#---------------------------------------
sex <- c("M","F")
age <- seq(30,50,by=10)
trt <- c("T","P")
dum1 <- expand.grid(sex=sex,age=age,trt=trt)
結果:
参照先: LINK
Post a Comment
別ページに移動します