0

I have small csv files in a directory. I need to calculate rmse and mse from each file.

For example inside Barcelona.csv

City, RealMax, ForecastMax
Barcelona, 30, 29
Barcelona, 31, 29
Barcelona, 32, 31
Barcelona, 29, 29
Barcelona, 27, 29

I have the next code by now in R

setwd("/home/enric/csv")
filenames <- list.files(pattern="*.csv", full.names=TRUE)  ###I open all the files csv
ldf <- lapply(filenames, read.csv, header=FALSE)
length(ldf)
ldf

foo <- function(x) {
# Function that returns Root Mean Squared Error
rmse <- function(error) {
sqrt(mean(error^2,na.rm = TRUE))
}

# Function that returns Mean Absolute Error
mae <- function(error) {
mean(abs(error))
}

error <- x$V2 - x$V3
mae <- mae(error)  
rmse <- rmse(error)   
return( list( mae = mae, rmse = rmse))
}

res <-lapply(ldf, foo)

and I get this message about factors...

> res <-lapply(ldf, foo)
Warning messages:
1: In Ops.factor(x$V2, x$V4) : - not meaningful for factors
2: In Ops.factor(x$V2, x$V4) : - not meaningful for factors

How can I convert factor to numerical in order to solve it? Well I think this is the problem, any help?

Enric Agud Pique
  • 1,087
  • 7
  • 13
  • 30

1 Answers1

0

Probably you have not read your data columns in as numeric. You can check the structure and classes of objects using str(). For converting while you're reading the file, look at the additional options of read.csv(), especially stringAsFactors. You can change the class after the fact by coercing to numeric: `as.numeric()'.

Community
  • 1
  • 1
noname
  • 473
  • 4
  • 11
  • and how can I save res[i] in different csv? for example, one file ErrorBarcelona.csv, and inside the name of Barcelona and the values mae and rmse, and the same for the other cities? – Enric Agud Pique Aug 28 '14 at 18:42
  • At the end, I have solved it partially with this code, but I have to introduce still inside every file the name of the city for (i in 1:6) { #I have 6 csv files write.table(res[i], file=paste(i, ".csv", sep="") ,row.names=TRUE, col.names=TRUE, sep=",") } – Enric Agud Pique Aug 29 '14 at 11:15