[英]R - for loop error: “Unexpected '}' in '}'”
here is the code for a simulation I'm trying to run:这是我尝试运行的模拟代码:
n_draws <- 1000
black <- rep(0, n_draws)
hispanic <- rep(0, n_draws)
asian <- rep(0, n_draws)
white <- rep(0, n_draws)
cutoff <- c(0.05,0.1,0.25,1)
draws <- runif(n_draws,0,1)
for (i in draws){
if (draws[i] < cutoff[1]){
black[i] <- 1
} else if ((draws[i] >= cutoff[1]) & (draws[i] < cutoff[2])){
hispanic[i] <- 1
} else if ((draws[i] >= cutoff[2]) & (draws[i] < cutoff[3]){
asian[i] <- 1
} else {
white[i] <- 1
}
}
Basically, I want to add a 1 to the corresponding list, conditional on where that number falls in the range (0,1).基本上,我想在相应的列表中添加一个 1,条件是该数字在 (0,1) 范围内的位置。 I'm not sure why this is giving an error.
我不确定为什么会出现错误。 Suggestions?
建议?
You're just missing a closing bracket just after cutoff[3]
, also used seq_along
in my example as it's a bit nicer您只是在
cutoff[3]
之后缺少一个seq_along
在我的示例中也使用了seq_along
因为它更好一些
for (i in seq_along(draws)){
if (draws[i] < cutoff[1]){
black[i] <- 1
} else if ((draws[i] >= cutoff[1]) & (draws[i] < cutoff[2])){
hispanic[i] <- 1
} else if ((draws[i] >= cutoff[2]) & (draws[i] < cutoff[3])){
asian[i] <- 1
} else {
white[i] <- 1
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.