Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
168 views
in Technique[技术] by (71.8m points)

r - Use names of data frames stored in list (within loop construct)

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!

question from:https://stackoverflow.com/questions/66061896/use-names-of-data-frames-stored-in-list-within-loop-construct

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 )


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...