0

I try to solve a problem from a question I have previously posted looping inside list in r

Is there a way to get the name of a dataframe that is on a list of dataframes? I have listed a serie of dataframes and to each dataframe I want to apply myfunction. But I do not know how to get the name of each dataframe in order to use it on nameofprocesseddf of myfunction.

Here is the way I get the list of my dataframes and the code I got until now. Any suggestion how I can make this work?

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=",")
}
3
  • try names(list_dataframes)[i] -- but where is the name being assigned? Commented Jun 14, 2016 at 13:51
  • that is exactly my problem I do not know how to assign the name of each dataframe contained on the list. Any idea how I can assign it? Commented Jun 14, 2016 at 13:54
  • llply comes from plyr, not dplyr. I think you will find that your object names are more trouble than they're worth. (I am referring to nonproceseddf, which contains a spelling error, et al, not the content of the question. Shorter names are generally better.) Commented Jun 14, 2016 at 15:52

1 Answer 1

1

as a matter of fact, the name of a data frame is not an attribute of the data frame. It's just an expression used to call the object. Hence the name of the data frame is indeed 'list_dataframes[i]'.

Since I assume you want to name your data frame as the text file is named without the extension, I propose you use something like (it require the library stringr) :

nameofprocesseddf = substr(dataframes[i],start = 1,stop = str_length(dataframes[i])-4)
Sign up to request clarification or add additional context in comments.

2 Comments

Is there an advantage to stringr::str_length over nchar here?
@Frank not really on normal string. but on some UTF-8 string, it might be different. nchar

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.