How can you concatenate two dataframes in r so that a new column appears in the resulting dataframe indicating the origin dataframe?
2 answers
The easiest way I see is (if you have a lot of datasets named df
+ some number, although you can choose another regex pattern as well) to get them all from the global environment and use the do.call/rbind
combination
res <- do.call(rbind, mget(ls(pattern = "^df\\d+$")))
res
# price size
# df1 10 40
# df2 20 50
If you prefer the source to be a column instead of row names, you could simply do
res$origin <- row.names(res)
+2
source to share