0

I want to find the RMSE between the columns in each dataframe present in the list. I have written below code:

i3<-2:6
for(iter in 1:length(filenames)){ # length(filename)=45
  for (i in seq_along(i3)){
    RMSE<-lapply(filelist, function(x) sqrt(mean(as.numeric(x[[iter]][[7]])-as.numeric(x[[iter]][[i]]))^2))
  }
}

But this code not working fine.

Dataframes is in below format:

df1:
    col2 col3 col4 col5 col6 col7 
    5     4    3     2     3  4     
    4     2    3    4      4  2      

df2:
    col2 col3 col4 col5 col6 col7  
    5     4    3     2     3  4     
    4     2    3    4      4  2   

I want o/p in the below form:

df1:
col2 col3 col4 col5 col6 col7 RMSE(7-2) RMSE(7-3) RMSE(7-4) RMSE(7-5) 
5     4    3     2     3  4     4.5        3.5     4.3        2.4
4     2    3    4      4  2      3.5       2        3.5        2.4

df2:
col2 col3 col4 col5 col6 col7 RMSE(7-2) RMSE(7-3) RMSE(7-4) RMSE(7-5) 
5     4    3     2     3  4     4.5        3.5     4.3        2.4
4     2    3    4      4  2      3.5       2        3.5        2.4

Thanks in advance!!

User0590
  • 103
  • 12
  • 1
    What isn't working about your code? Can you make your example reproducible? [See here for tips](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) Use either (a) built-in data, (b) simulated data (share code and use `set.seed` to control randomness), or (c) use `dput()` to share a small, copy/pasteable subset of your data. – Gregor Thomas Aug 18 '17 at 19:43
  • @Gregor, it's not possible to put data over here because it quiet big. If you didn't understand, I can clarify more on it. – User0590 Aug 18 '17 at 20:16
  • @Gregor, I shared subset of dataframes in question. – User0590 Aug 18 '17 at 20:36

1 Answers1

0

You might want to look in to purrr:map() or map2()

Here I've refactored your code a bit... it still isn't going to work, but this might be a bit closer to what you want.

RMSE <- list()

for(i in 1:length(filenames)) {
    df <- read.file(filenames[i])
    for(j in 2:6) {
        RMSE[i] <- lapply(df, fun)
    }
}
Alex P
  • 1,574
  • 13
  • 28
  • I am getting this **Error in .subset2(x, i, exact = exact) : subscript out of bounds** while running the below code: i3<-2:6 for(iter in 1:length(filenames)){ for (i in seq_along(i3)){ a[iter]<-lapply(filelist, function(x) sqrt(mean(as.numeric(x[[iter]][[7]])-as.numeric(x[[iter]][[i]])))^2) } } – User0590 Aug 18 '17 at 20:10