2

I have a script written on Basic, that get on input CSV-file and compute collision between random splitting of input data. This script is here. I need to reimplement it on R. I've written such script. Here is input data.

But in condition

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.")  
}

appear error

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

Why is the error here? And is there other errors in R file?

UPDATE I've forgotten write in question about error

Error in seq.default(1, firstsofar, 1) : Invalid sign 'by' argument

in fragment of code

for (q in seq(1,firstsofar,1)) {
    if( randnum[i]==randnum[q]) {taken="yes"}
}
Mikael Engver
  • 4,634
  • 4
  • 46
  • 53
skeeph
  • 462
  • 2
  • 6
  • 18
  • 2
    As for your edit, it's completely unrelated to your original question, so to get an answer you should start a new question. But it sounds like `firstsofar` took a value less than 1, so that the 3rd argument `by=1` that you've given is the wrong sign (positive, not negative) to get the sequence there. – Gregor Thomas Feb 02 '13 at 19:53

2 Answers2

3

Rewrite that as

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.")
}

You need to have the else on the same line as the closing brace for the 'if'

From the R Language Definition

The else clause is optional. The statement if(any(x <= 0)) x <- x[x <= 0] is valid. When the if statement is not in a block the else, if present, must appear on the same line as the end of statement2. Otherwise the new line at the end of statement2 completes the if and yields a syntactically complete statement that is evaluated. A simple solution is to use a compound statement wrapped in braces, putting the else on the same line as the closing brace that marks the end of the statement.

Dason
  • 60,663
  • 9
  • 131
  • 148
2

For reasons I don't entirely understand, R freaks out when else isn't on the same line as the closing curly bracket before it. Try:

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.")
}