I have a few datasets that I'm merging together with bind_rows. My code looks like this
df1 <- data.frame(v1 = 1:10, v2 = 11:20)
df2 <- data.frame(v1 = 11:20, v2 = 1:10)
list(df1, df2) %>%
bind_rows(.id = "name")
I want the name
column to read "df1"
for df1
and "df2"
for df2
. I know I can use set_names()
to do this manually...
list(df1, df2) %>%
set_names(c("df1", "df2")) %>%
bind_rows(.id = "name")
...but I want a way to do this programatically that works no matter what the dataset names are. How can I do this?