我试图让一个脚本来生成随机的一组使用R.与人口统计信息的人,我希望它按行产生,而不是列,这样的功能可以基于同一行中前一个函数的结果。我知道这可以用做对环(像我一样下文),但for循环非常慢R.我已阅读,你可以使用申请或而以更有效地做一个循环,但我的天堂”尽管许多尝试失败,但我仍然想到了如何。以下是带有循环的功能代码示例。我将如何做到这一点与适用或而?替代for循环的唯一行,以填补data.frame
y <- 1980 ## MedianYr
d <- 0.1 ## Rate of NA responses
AgeFn <- function(y){
Year <- 1900 + as.POSIXlt(Sys.Date())$year
RNormYr <- as.integer((rnorm(1)*10+y))
Age <- Year - RNormYr
}
EduByAge <- function (Age, d) {
ifelse(Age < 17, sample(c("Some High School",NA), size=1,prob=c((1-d),d)),
ifelse(Age > 16 & Age < 19, sample(c("Some High School", "High School Grad",NA), size=1, prob=c(0.085, 0.604,d)),
ifelse(Age > 18 & Age < 21, sample(c("Some High School", "High School Grad", "Associates",NA), size=1,prob=c(0.085, 0.25, 0.354,d)),
ifelse(20 > Age & Age < 23, sample(c("Some High School", "High School Grad", "Associates", "Bachelors",NA), size=1,prob=c(0.085, 0.25, 0.075, 0.279,d)),
ifelse(Age > 22, sample(c("Some High School", "High School Grad", "Associates", "Bachelors", "Masters", "Professional", "Doctorate",NA),size=1,prob=c(0.085, 0.25, 0.075, 0.176, 0.072, 0.019, 0.012,d)), NA)))))
}
GenderFn <- function(d){
Gender1 <- sample(c("Male","Female","Trans", NA), 1, replace=TRUE, prob=c(0.49, 0.5, 0.01, d))
return(Gender1)
}
UserGen <- function(n,s) {
set.seed(s)
Rows <- function(y,d){
Age <- abs(AgeFn(y))
Gender <- GenderFn(d)
Education <- EduByAge(Age,d)
c(i, Age, Gender, Education)
}
df <- data.frame(matrix(NA, ncol = 4, nrow = n))
for(i in (1:n)) {
df[i,] <- Rows(y,d)
}
colnames(df) <- c("ID", "Age", "Gender", "Education")
return(df)
}
它看起来不像你的函数有任何从它们返回的东西。例如,'AgeFn'似乎没有返回值。 – TARehman 2013-03-06 21:11:39
@Tarehman来自'?“function”':“如果在不调用'return'的情况下达到某个函数的结尾,则返回上一个计算过的表达式的值。” – 2013-03-06 21:20:00
@BlueMagister Duh,我总是忘记了关于R.的错误。 – TARehman 2013-03-06 21:20:46