【发布时间】:2016-06-14 13:31:26
【问题描述】:
我尝试从我之前发布的问题中解决问题looping inside list in r
有没有办法获取数据框列表中的数据框的名称?
我列出了一系列数据帧以及我想应用myfunction 的每个数据帧。但是我不知道如何获取每个数据框的名称以便在myfunction 的nameofprocesseddf 上使用它。
这是我获取数据框列表和到目前为止获得的代码的方式。有什么建议可以让我完成这项工作吗?
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=",")
}
【问题讨论】:
-
试试
names(list_dataframes)[i]-- 但是分配的名称在哪里? -
这正是我的问题我不知道如何分配列表中包含的每个数据框的名称。知道如何分配它吗?
-
llply来自 plyr,而不是 dplyr。我想你会发现你的对象名称比它们的价值更麻烦。 (我指的是nonproceseddf,其中包含拼写错误等,而不是问题的内容。较短的名称通常更好。)