R“for loop”错误讯息{} [英] R "for loop" error messages {}
问题描述
由于几天前我在R中运行for循环,它给了我很多与}相关的错误。它只发生在我突出显示整个代码并运行它。如果我一行一行地执行,那么它运行得很好。
Since a couple of days ago when I run a for loop in R it gives me plenty of errors related to "}". It only happens if I highlight the whole code and run it. If i execute it line by line, then it runs just fine.
我尝试了最基本的循环:
I tried even with the most basic loop:
foo <- seq(1, 100, by=2)
foo.squared <- NULL
for (i in 1:50 ) {
foo.squared[i] <- foo[i]^2
}
这是控制台:
> foo <- seq(1, 100, by=2)
"rror: unexpected input in "foo <- seq(1, 100, by=2)
> foo.squared <- NULL
"rror: unexpected input in "foo.squared <- NULL
> for (i in 1:50 ){
"rror: unexpected input in "for (i in 1:50 ){
> foo.squared[i] <- foo[i]^2
"rror: unexpected input in " foo.squared[i] <- foo[i]^2
> }
Error: unexpected '}' in "}"
>
R会话的详细信息(我在RStudio中运行):
Details of the R session (I run it in RStudio):
> sessionInfo()
R version 3.0.2 (2013-09-25)
Platform: x86_64-apple-darwin10.8.0 (64-bit)
一直非常讨厌!我会感谢任何建议!
谢谢,
Maria
It has been very annoying!! I would appreciate any advice!!! Thanks, Maria
更新:
开始我怀疑导致这个问题。它应该采取一个名称的向量,并从中提取第二个元素。
Here is the code in the very beginning that I suspect causes this problem..It is supposed to take a vector of names and extract the second element from it.
splitnames <- strsplit(as.character(train$Name),"[,.]")
firstelement <- function(x){x[2]}
sapply(splitnames,firstelement)
执行后,R动作很奇怪。虽然我不是100%肯定的。
After I execute it R acts weird. Though I am not 100% sure.
推荐答案
我遇到了同样的问题,并发现它是由RStudio中的一个错误引起的(代码运行良好在R和R-gui中,但是在RStudio中失败。)在RStudio的保存状态中的某些事情被破坏之前很难再现,之后行为是非常一致的。
I have been experiencing the same problem, and have found that it is caused by a bug in RStudio (the code runs fine in R and R-gui, but fails in RStudio.) It is hard to reproduce until something gets corrupted in RStudio's saved state, after which the behaviour is pretty consistent.
删除〜/ .rstudio-desktop
修复了我的问题。
mv ~/.rstudio-desktop ~/rstudio-desktop.old
更多关于重新设置RStudio的各种状态平台 here。
More on resetting RStudio's state on various platforms here.
这篇关于R“for loop”错误讯息{}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!