This question already has answers here:
Combine several data frames in the global environment by row (rbind)
(2 answers)
Closed 7 years ago.
I have a problem with rbind data sets.
For example, data set names are like this:
data_1, data_2, data_3,...,data_100
data set number is not fixed. Sometimes 100, sometimes 250.
My method is just write every data sets (a <- rbind(data_1, data_2, ... , data_100).
I want to make function for use loop but I couldn't make it.
We can use pattern argument in ls to get the object names that matches the pattern as a string, then we use mget to get the values in a list and finally rbind the list elements with do.call.
lst <- mget(ls(pattern='data_\\d+'))
do.call(rbind, list)
Or we can use rbindlist
library(data.table)
rbindlist(lst)
Or bind_rows from dplyr
library(dplyr)
bind_rows(lst)
Related
This question already has answers here:
Combine several data frames in the global environment by row (rbind)
(2 answers)
How to rbind all the data.frames in your working environment?
(1 answer)
Closed 2 years ago.
I have 5 dataframes all with the same heading and columns
df1,df2,df3,df4,df5
I created a list based on the dataframe names
List<-c("df1","df2","df3","df4",d"f5")
I want to create an rbind looping the df's based off the list. i tried this
ALL_dfs<-lapply(List,rbind)
but this doesn't read it as a combined dataframe by rbind function.
I think the most straightforward option is this:
do.call(rbind, mget(List))
?mget
Search by name for an object (get) or zero or more objects (mget).
The other option, if possible, is to store your data.frames in a list when you create them. Not always an option, but may be possible with some planning
With map_df from purrr.
library(purrr)
all_dfs <- map_dfr(.x = List, .f = bindr)
This question already has answers here:
Combine a list of data frames into one data frame by row
(10 answers)
Closed 2 years ago.
Just a question on rbind.
When running df_all <- rbind(df_1, df_2 ...) to combine multiple dataframes, I was wondering whether is it possible to add in a separate column that includes the names of the individual dataframes where each observation originates from?
Many Thanks,
Mervyn
Try this approach :
library(dplyr)
new_df <- bind_rows(lst(df_1, df_2), .id = 'id')
Similarly, if there are lot of such dataframes you don't need to write them one by one. Create a string vector using paste0 and then use mget + bind_rows.
new_df <- bind_rows(mget(paste0('df_', 1:2)), .id = 'id')
You can change 2 to whatever number of dataframes that you have in your global environment.
This question already has answers here:
How do I make a list of data frames?
(10 answers)
Closed 4 years ago.
I have around 500 dataframes with two columns and a varying number of rows (from 10 till 30) in R. I also have a character vector containing the names for the dataframes. I now wish to bundle all these dataframes into a single list so I can refer to these list elements using the dataframe names.
I was thinking to use a loop to loop through the dataframes and add them to the list in each step but I couldn't pull it of.
The following works:
df_list = mget(df_names)
However, the real solution is to not have 500 data.frames in your workspace to begin with: either load/create them immediately inside a list, or have your data in such a format that it’s collated into one big data.frame.
Think of variables as things you have to keep in your head at the same time: if there are too many to remember, you have too many. Because how else can you reason about the state of your program, i.e. about its semantics and correctness?
Check this:
df_names <- c( ) # yours dataframes names character vector
lst <- list()
for (i in df_names) {
lst[i] <- list(get(i))
}
This question already has answers here:
rbind data frames based on a common pattern in data frame name
(2 answers)
Closed 4 years ago.
I have 100 data frames called df1, df2, df3 ...... df100. How can I use the rbind function without typing the name of every single data frame?
You can do paste0("df", 1:100) to generate the vector of the dataframe names. Then mget(paste0("df", 1:100)) gives the dataframes in a list. And you can use do.call to call rbind on this list; finally the command is:
do.call(rbind, mget(paste0("df", 1:100)))
This question already has answers here:
Combine several data frames in the global environment by row (rbind)
(2 answers)
Closed 6 years ago.
I need to rbind varying number of datasets (data frames).
Currently I have this:
dom.ready.dat <- rbind(dom.ready.dat.bad1,
dom.ready.dat.bad2,
dom.ready.dat.bad3,
dom.ready.dat.bad4,
dom.ready.dat.bad5,
dom.ready.dat.bad6,
dom.ready.dat.bad7,
dom.ready.dat.bad8,
dom.ready.dat.bad9,
dom.ready.dat.bad10,
dom.ready.dat.good1,
dom.ready.dat.good2,
dom.ready.dat.good3,
dom.ready.dat.good4,
dom.ready.dat.good5,
dom.ready.dat.good6,
dom.ready.dat.good7,
dom.ready.dat.good8,
dom.ready.dat.good9,
dom.ready.dat.good10)
However, I might have more than 10 data frames so I need to refer to the pattern dom.ready.dat* dynamically adding the suffix dynamically to some function in R that gets varying number of parameters and assign a function like rbind on all of them
If you're creating the data.frames through lapply() you can pipe the resulting list through to rbind_all from dplyr:
library(dplyr)
lapply(1:20, function(i){
read.csv(sprintf("some_file%s.csv", i))
}) %>%
rbind_all
This way you don't have the intermediate step of 20 different data.frames if you're only interested in the combined.
We can specify the pattern in ls to get all the objects that were created in the global environment as a string, then use mget to get the values in the objects as a list. From there, either use rbindlist from data.table
library(data.table)
rbindlist(mget(ls(pattern="^dom.ready.dat")))
Or use rbind
do.call(rbind, mget(ls(pattern="^dom.ready.dat")))
A better option would be to read all the files in a list and then rbind instead of creating lots of objects.
files <- list.files(pattern="somepattern.csv", full.names=TRUE)
library(data.table)
rbindlist(lapply(files, fread))