我想要找到列表中每个数据帧中列之间的RMSE。我写了以下代码:
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))
}
}
但是这段代码并不能很好地工作。
数据帧格式如下:
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
我想要以下形式的o/p:
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
提前感谢!!
发布于 2017-08-19 03:45:01
您可能需要查看purrr:map()
或map2()
在这里,我对您的代码进行了一些重构...它仍然不会工作,但这可能会更接近你想要的。
RMSE <- list()
for(i in 1:length(filenames)) {
df <- read.file(filenames[i])
for(j in 2:6) {
RMSE[i] <- lapply(df, fun)
}
}
https://stackoverflow.com/questions/45763769
复制相似问题