rbind
Combine several data frames in the global environment by row (rbind)
Since you have already read the files in, you can try the following: do.call(rbind, mget(ls(pattern = “df”))) The ls(pattern = df) should capture all of your “df.1”, “df.2”, and so on. Hopefully you don’t have other things named with the same pattern, but if you do, experiment with a stricter pattern until the command lists … Read more
rbind data frames based on a common pattern in data frame name
We can use ls with mget library(data.table) rbindlist(mget(ls(pattern = “^df\\.\\d+”))) Or with dplyr library(dplyr) mget(ls(pattern=”^df\\.\\d+”)) %>% bind_rows() Or with rbind from base R do.call(rbind, mget(ls(pattern=”^df\\.\\d+”)))
How can I prevent rbind() from geting really slow as dataframe grows larger?
You are in the 2nd circle of hell, namely failing to pre-allocate data structures. Growing objects in this fashion is a Very Very Bad Thing in R. Either pre-allocate and insert: df <- data.frame(x = rep(NA,20000),y = rep(NA,20000)) or restructure your code to avoid this sort of incremental addition of rows. As discussed at the … Read more
R – use rbind on multiple variables with similar names
That is the wrong way to handle related items. Better to use a list or dataframe, but you will probably find out why in due course. For now: do.matrix <- do.call(rbind, lapply( ls(patt=”variable”), get) ) Or: do.matrix <- do.call(rbind, lapply( paste0(“variable”, 1:10) , get) )
Converting nested list to dataframe
You can also use (at least v1.9.3) of rbindlist in the data.table package: library(data.table) rbindlist(mylist, fill=TRUE) ## Hit Project Year Rating Launch ID Dept Error ## 1: True Blue 2011 4 26 Jan 2012 19 1, 2, 4 NA ## 2: False NA NA NA NA NA NA Record not found ## 3: True Green … Read more
Simplest way to get rbind to ignore column names
You might find setNames handy here… rbind(df, setNames(rev(df), names(df))) # x y #1 1 3 #2 2 4 #3 3 1 #4 4 2 I suspect your real use-case is somewhat more complex. You can of course reorder columns in the first argument of setNames as you wish, just use names(df) in the second argument, … Read more
rbind dataframes with a different column name
My favourite use of mapply: Example Data a <- data.frame(a=runif(5), b=runif(5)) > a a b 1 0.8403348 0.1579255 2 0.4759767 0.8182902 3 0.8091875 0.1080651 4 0.9846333 0.7035959 5 0.2153991 0.8744136 and b b <- data.frame(c=runif(5), d=runif(5)) > b c d 1 0.7604137 0.9753853 2 0.7553924 0.1210260 3 0.7315970 0.6196829 4 0.5619395 0.1120331 5 0.5711995 0.7252631 … Read more
What’s wrong with my function to load multiple .csv files into single dataframe in R using rbind?
There’s a lot of unnecessary code in your function. You can simplify it to: load_data <- function(path) { files <- dir(path, pattern = ‘\\.csv’, full.names = TRUE) tables <- lapply(files, read.csv) do.call(rbind, tables) } pollutantmean <- load_data(“specdata”) Be aware that do.call + rbind is relatively slow. You might find dplyr::bind_rows or data.table::rbindlist to be substantially … Read more
Why is rbindlist “better” than rbind?
rbindlist is an optimized version of do.call(rbind, list(…)), which is known for being slow when using rbind.data.frame Where does it really excel Some questions that show where rbindlist shines are Fast vectorized merge of list of data.frames by row Trouble converting long list of data.frames (~1 million) to single data.frame using do.call and ldply These … Read more