2013-02-02 87 views
2

我有一个写在Basic上的脚本,它获取输入CSV文件并计算输入数据随机分割之间的冲突。这个脚本是here。我需要在R上重新实现它。我写了such脚本。 Here是输入数据。调试R脚本

但条件

if(nentries==nrows*ncolumns)  
{ 
    print("Columns, rows, and entries check; we're good to go.") 
} 
else 
{ 
    print("Columns, rows, and entries don't check; please look at your data file to make sure each line has equal no. of entries.") 
} 

出现错误

source("path\\to\\script.r") 
error in source("path\\to\\script.r") : 
D:\projects\basicToR\target.r:19:1: Unexpected 'else' 
18:   } 
19: else 
    ^

为什么是错误吗? R文件中是否还有其他错误?

UPDATE 我忘了写在问题有关错误seq.default

错误(1,firstsofar,1): '通过' 的说法

中的代码片段

无效 迹象
for (q in seq(1,firstsofar,1)) { 
    if(randnum[i]==randnum[q]) {taken="yes"} 
} 
+2

至于你的编辑,它是完全无关你的原始问题,所以要得到答案,你应该开始一个新的问题。但是这听起来像'firstsofar'取小于1的值,所以你给出的第三个参数'by = 1'是错误的符号(正数,而不是负数)以获得序列。 – Gregor

回答

2

由于我不完全明白的原因,当R else与之前的结束花括号不在同一行时,R就吓了一跳。尝试:

if(nentries==nrows*ncolumns) { 
    print("Columns, rows, and entries check; we're good to go.") 
}else{ 
    print("Columns, rows, and entries don't check; please look at your data file to make sure each line has equal no. of entries.") 
} 
+2

这是REPL - 逐行解析。 –

+2

原因是R代码一次只运行一行,无论您是否在控制台上。这是你可以输入(在'>'控制台)同样的原因 '> 1 +',然后 '> 2', 但不 '> 1' '> + 2' – Gregor

+0

感谢,@DirkEddelbuettel和shujaa澄清。 – 2013-02-02 18:11:07

3

重写,作为

if(nentries==nrows*ncolumns) { 
    print("Columns, rows, and entries check; we're good to go.") 
}else{ 
    print("Columns, rows, and entries don't check; please look at your data file to make sure each line has equal no. of entries.") 
} 

你需要有其他人在同一行右括号的“如果”

R Language Definition

else子句是可选的。 (任何(x< = 0))×<-x [x< = 0]的声明是有效的。当if语句不在块中时,如果存在 ,则必须出现在与statement2的结尾相同的行上。 否则,在statement2结尾处的新行完成if和 会生成一个经过评估的语法上完整的语句。一个简单的 解决方案是使用包装在大括号中的复合语句,将 其他语句放在与标记 语句末尾的右括号相同的行上。