For loops in R (unexpected symbol error)

7.7k views Asked by At

I need to calculate the overallMin in across 5 files that I have downloaded. The filenames all have the same format:

"2013-07-citibike.csv" "2013-08-citibike.csv"

"2013-09-citibike.csv" "2013-10-citibike.csv"

"2013-11-citibike.csv" "2013-12-citibike.csv"

I need to write a for loop that reads these files, calculates the min (of one particular column) for each file and then the overall min from all files.

So far I have this

numbers <- 07:12
filenames <- paste("2013-", numbers, "-citibike", ".csv", sep="")

overallMin <- 0
for (i in filenames) {
    trips <- read.csv(i)
    newMin <- min(trips)
    if (overallMin < newMin) {
        overallMin <- newMin
    }
}
overallMin

Confused with the csv part. Plus I am getting an Error: unexpected symbol in "for (i in filenames) { trips = read.csv(i) newMin"

2

There are 2 answers

5
Robert On BEST ANSWER

Define the file names directly. Another thing, you need to specify the column number, or you want the minimum of all file?

filenames <-c("2013-07-citibike.csv", "2013-08-citibike.csv",
              "2013-09-citibike.csv", "2013-10-citibike.csv",
              "2013-11-citibike.csv", "2013-12-citibike.csv")

or use sprintf to get the correct file names

numbers <- 07:12
filenames <- paste("2013-", sprintf("%02d",numbers), "-citibike", ".csv", sep="")

overallMin <- 1e80 #big number or the minimum will probably be zero
desirecol <- 2
for (i in filenames) {
  trips <- read.csv(i)
  newMin <- min(trips[,desirecol])
  if (overallMin < newMin) {
    overallMin <- newMin
  }
}
overallMin
4
IRTFM On

The error message "unexpected symbol" almost always means a missing comma or parenthesis or some other silly syntax error. I must disagree with the thrust of @BrandonBertelson's comment. Reading error messages is usually helpful. In this case it is helpful to us in knowing the either there was an inappropriate substitution of I for i or that the poster did not post the exact error message or that there is a non-printing character that his editor inserted. I'm not able to reproduce the error by substituting an I for an i, since there is an I() function and the parser is correctly pointing out to me that I gave read.csv() an argument of the wrong type.

> for( i in 1:5) { my <- read.csv(I)}
Error in read.table(file = file, header = header, sep = sep, quote = quote,  : 
  'file' must be a character string or connection

The theory that the file names are wrong (and I agree that the code should have used list.files does NOT fit the error message. I can duplicate the error message by simply putting the first two lines on the same line:

> for( i in 1:5) { my <- read.csv(i) newMin <- min(trips[,desirecol])}
Error: unexpected symbol in "for( i in 1:5) { my <- read.csv(i) newMin"

For the specific question (aside form the error that remains obscure), this should succeed:

yourpath <- insert your path or getwd() if in working directory
colname  <- replace_with_column_name

min(                           #min of all the mins
    sapply( list.files( yourpath, pattern="citibike.*csv$),  #regex match
     function(f) min( read.csv(f)[[colname]]   # the columns rather than a whole dataframe
                       )  # returns the single mins
    )