在数据框列表中使用数据框的名称

use name of dataframe on a list of dataframes

我尝试解决我之前发布的问题 looping inside list in r

有没有办法获取数据框列表中数据框的名称? 我已经列出了一系列数据帧和我想要应用的每个数据帧 myfunction。但我不知道如何获取每个数据帧的名称以便在 myfunctionnameofprocesseddf 上使用它。

这是我获取我的数据帧列表和我到现在为止获得的代码的方式。有什么建议可以让我完成这项工作吗?

library(missForest)
library(dplyr)

myfunction <- function (originaldf, proceseddf, nonproceseddf, nameofprocesseddf=character){
NRMSE <- nrmse(proceseddf, nonproceseddf, originaldf)
comment(nameofprocesseddf) <- nameofprocesseddf
results <- as.data.frame(list(comment(nameofprocesseddf), NRMSE))
names(results) <- c("Dataset", "NRMSE")
return(results)
}
a <- data.frame(value = rnorm(100), cat = c(rep(1,50), rep(2,50)))
da1 <- data.frame(value = rnorm(100,4), cat2 = c(rep(2,50), rep(3,50)))

dataframes <- dir(pattern = ".txt") 
list_dataframes <- llply(dataframes, read.table, header = T, dec=".", sep=",")
n <- length(dataframes)

# Here is where I do not know how to get the name of the `i` dataframe 
for (i in 1:n){
modified_list <- llply(list_dataframes, myfunction, originaldf = a, nonproceseddf = da1, proceseddf = list_dataframes[i], nameof processeddf= names(list_dataframes[i]))
write.table(file = sprintf("myfile/%s_NRMSE20%02d.txt", dataframes[i]), modified_list[[i]], row.names = F, sep=",")
}

事实上,数据框的名称并不是数据框的属性。它只是用于调用对象的表达式。因此数据框的 name 确实是 'list_dataframes[i]'.

因为我假设你想命名你的数据框,因为文本文件没有扩展名,我建议你使用类似的东西(它需要库 stringr):

nameofprocesseddf = substr(dataframes[i],start = 1,stop = str_length(dataframes[i])-4)