1

I retrieve all dataframes from environment and then do some stuff:

dfs <- Filter(function(x) is(x, "data.frame"), mget(ls()))

names(dfs)

"customers"
"sales"
"campaigns"

First thing I need to extract is object name inside a loop:

for (df in dfs) {
  df_name <- deparse(substitute(df))
  # do some stuff
  # do some more stuff
  print(df_name)
}

But instead data frame names to operate I get:

"df"
"df"
"df"

I've tested a function inside the loop too:

find_name <- function(df) {
  df_name_is <- substitute(df)
  return(df_name_is)
}

But the output is:

df
df
df

looping through names of df gets me the colnames of every df, not the df name itself.

any hint will be much appreciated

2
  • looping through names of df gets me the colnames of every df Commented Feb 14, 2017 at 14:43
  • bouncyball print(df) returns the complete content for every df but I just need the name Commented Feb 14, 2017 at 15:32

2 Answers 2

5

If you don't mind use loop by counter (length) instead of names this is a valid approach:

for (i in 1:length(dfs)) {

print(names(dfs)[i])

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

Comments

2

What about iterating over the names?!

for (df_name in names(dfs)) {
  print(df_name)
  df_obj <- dfs[[df_nmae]]
}

Comments

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.