-4

我试图运行这些代码,但被卡住了..

a<-data.frame(1,2,3,4,5,6,7,8,9,10,11,12,13,14)
n<-1
empty <- data.frame(stringsAsFactors=F)
while (n<=5) {
    if (sum((a[,2*n+3]), (a[,2*n+4])) == 13) {
        empty <-data.frame(cbind(empty, 3), n<-n+1
    } else {
        empty <-data.frame(cbind(empty, 2), n<-n+1
    }
}

这些是我得到的:

> a<-data.frame(1,2,3,4,5,6,7,8,9,10,11,12,13,14)
> n<-1
> empty <- data.frame(stringsAsFactors=F)
> while (n<=5) {
+     if (sum((a[,2*n+3]), (a[,2*n+4])) == 13) {
+         empty <-data.frame(cbind(empty, 3), n<-n+1
+     } else {
Error: unexpected '}' in:
"        empty <-data.frame(cbind(empty, 3), n<-n+1
    }"
>         empty <-data.frame(cbind(empty, 2), n<-n+1
+     }
Error: unexpected '}' in:
"        empty <-data.frame(cbind(empty, 2), n<-n+1
    }"
> }
Error: unexpected '}' in "}"

我已经尝试了许多去掉“{}”的组合,甚至将代码写入同一行,有或没有这些“{}”,它们都不起作用。我的代码有什么问题?

4

2 回答 2

2

这是你想要的吗?

a<-data.frame(1,2,3,4,5,6,7,8,9,10,11,12,13,14)
n<-1
empty <- c()
while (n<=5) {
cat("steps",n,"\n")
    if (sum((a[,((2*n)+3)]), (a[,((2*n)+4)])) == 13) {
cat("true in step",n,"\n")
        empty <-cbind(empty, 3)
        } else {
cat("false in step",n,"\n")
        empty <-cbind(empty, 2)                         
    }
    n<-n+1  # this should be after the if condition
}



  steps 1 
false in step 1 
steps 2 
false in step 2 
steps 3 
false in step 3 
steps 4 
false in step 4 
steps 5 
false in step 5 
> empty
     [,1] [,2] [,3] [,4] [,5]
[1,]    2    2    2    2    2
于 2013-08-14T23:43:47.680 回答
2

似乎您在以下位置有不平衡的括号:

empty <-data.frame(cbind(empty, 3), n<-n+1
于 2013-08-14T23:17:10.167 回答