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+")))
R - rbind multiple dataframes in list based on pattern in dataframe name
We can find out which list have "01"
in their names using grepl
(vals
). We then create a new list, one with all vals
index together and the other one without them and assign them names.
vals <- grepl("01", names(list_df))
setNames(list(do.call(rbind, list_df[vals]),
do.call(rbind, list_df[!vals])), new_list)
using rbind to combine all data sets the names of all data set start with common characters
Try first obtaining a vector of all matching objects using ls()
with the pattern ^test
:
dfs <- lapply(ls(pattern="^test"), function(x) get(x))
result <- rbindlist(dfs)
I am taking the suggestion by @Rohit to use rbindlist
to make our lives easier to rbind
together a list of data frames.
Combine two data frames by rows (rbind) when they have different sets of columns
rbind.fill
from the package plyr
might be what you are looking for.
How to bind multiple dataframes in R
If names
store the object names of objects created in the global environment, then we need the value of those. We can get those with mget
in a list
library(dplyr)
bind_rows(mget(names), .id = 'id')
rbind dataframes with a different column name
You could use rbindlist
which takes different column names. Using @LyzandeR's data
library(data.table) #data.table_1.9.5
rbindlist(list(a,b))
# 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
# 6: 0.7604137 0.9753853
# 7: 0.7553924 0.1210260
# 8: 0.7315970 0.6196829
# 9: 0.5619395 0.1120331
#10: 0.5711995 0.7252631
Update
Based on the object names of the 12 datasets (i.e. 'Goal1_Costo', 'Goal2_Costo',..., 'Goal12_Costo'),
nm1 <- paste(paste0('Goal', 1:12), 'Costo', sep="_")
#or using `sprintf`
#nm1 <- sprintf('%s%d_%s', 'Goal', 1:12, 'Costo')
rbindlist(mget(nm1))
rbind dataframe rows with common IDs
You first get common ID
s using intersect
and then subset and rbind
both dataframes -
valid_ids <- intersect(df1$ID, df2$ID)
rbind(df1[df1$ID %in% valid_ids, ], df2[df2$ID %in% valid_ids, ])
ID A B C
1 1 0 0 0
2 1 1 0 1
3 1 49 49 32
Related Topics
What Ides Are Available for R in Linux
Printing Newlines with Print() in R
Data.Table and Parallel Computing
How to Directly Select the Same Column from All Nested Lists Within a List
Duplicate 'Row.Names' Are Not Allowed Error
Add Objects to Package Namespace
R: Assign Variable Labels of Data Frame Columns
Mean of a Column in a Data Frame, Given the Column's Name
Is There a Way of Manipulating Ggplot Scale Breaks and Labels
Getting Over Query Limit After One Request with Geocode
Way to Securely Give a Password to R Application from the Terminal
Add Extra Level to Factors in Dataframe
Add (Subtract) Months Without Exceeding the Last Day of the New Month