R-for循环错误:“'}'中出现意外的'}'”

时间:2020-09-03 16:26:39

标签: r for-loop if-statement simulation

这是我要运行的模拟代码:

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

 }
}

基本上,我想在对应的列表中添加1,条件是该数字在(0,1)范围内。我不确定为什么会出错。有建议吗?

1 个答案:

答案 0 :(得分:0)

您只是在 string[] inputs = { "name0.name1@xyz.com", "name0.name1@xyz.com", "name0.name1" }; string pattern = "(?'path'.*)@(?'site'.*)"; foreach(string input in inputs) { Match match = Regex.Match(input,pattern); string[] paths = match.Groups["path"].Value.Split(new char[] {'.'}).ToArray(); string site = match.Groups["site"].Value; Console.WriteLine("Path : '{0}', Site : '{1}'", string.Join("-", paths), site); } Console.ReadLine(); 之后缺少了一个右括号,在我的示例中也使用了cutoff[3],因为它更好一些

seq_along