0

I am currently experiencing issues with a loop construct of mine. I loop over several data frames that are stored in a list. For each iteration, I would like to extract the name of the data frame to be then used as output name. I tried the following:

df_list<-list(df1, df2, df3)
#Looping across list of data frames ...
#Extract name of data frame:
name<-paste0(as.character(df_list[[i]]), "result")

However, this approach extract the complete content of the data frame and uses it as variable name. I tried several other approaches, but all failed...

Thank you very much in advance!

3
  • what does names(df_list) return? Commented Feb 5, 2021 at 11:06
  • It returns NULL. Commented Feb 5, 2021 at 11:07
  • By default, your list of data frames will not have names, you can provide it using names(df_list) <- c("df1", "df2", "df3") Commented Feb 5, 2021 at 11:08

1 Answer 1

0

Use tibble::lst() to create the list.. this function automatically uses the 'name' of the added object as name in the list, so it always returns a named list.

so df_list <- tibble::lst( df1, df2, df3 )

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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