我在一個目錄中有小的csv文件。我需要從每個文件中計算rmse和mse。使用lapply計算rmse
例如內Barcelona.csv
City, RealMax, ForecastMax
Barcelona, 30, 29
Barcelona, 31, 29
Barcelona, 32, 31
Barcelona, 29, 29
Barcelona, 27, 29
我現在有一個代碼中的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)
和我得到的因素此消息...
> 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
如何將因子轉換爲數值來解決它?那麼我認爲這是問題,有什麼幫助?
爲什麼使用header = FALSE當你有一個頭?整個csv文件正在被讀入字符串,現在轉換爲因子 – rawr 2014-08-28 17:36:04
,是的!我已更改爲TRUE – 2014-08-29 06:53:32