有没有办法获取数据框列表上的数据框的名称?我列出了一系列数据框以及要应用的每个数据框myfunction
。但我不知道怎么才能把它用在获取每个数据帧的名字nameofprocesseddf
的myfunction
。
这是我获取数据框列表和到目前为止获得的代码的方式。有什么建议可以使我工作吗?
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=",")
}
实际上,数据帧的名称不是该数据帧的属性。它只是用于调用对象的表达式。因此,数据帧的名称确实是'list_dataframes [i]'。
由于我假设您要命名数据框架,因为文本文件是不带扩展名的,所以我建议您使用类似的东西(它需要库字符串):
nameofprocesseddf = substr(dataframes[i],start = 1,stop = str_length(dataframes[i])-4)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句