2013-02-02 85 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和 會生成一個經過評估的語法上完整的語句。一個簡單的 解決方案是使用包裝在大括號中的複合語句,將 其他語句放在與標記 語句末尾的右括號相同的行上。