Combining data frames that are named consecutively [duplicate] - r

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)))

Related

How to loop through specific data frames based on a List and add them all in a rbind function using R? [duplicate]

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)

combining dataframes using rbind + adding a separate column that includes the names of the individual dataframes [duplicate]

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.

R, join two columns in a data frame nested within a list [duplicate]

This question already has answers here:
How to use lapply and paste on multiple dataframes in a list
(2 answers)
Closed 4 years ago.
There is a list (named d) of structurally identical data frames
Trying to create a new column in each of the data frames that joins two columns
I keep getting an invalid 'digits' argument with the following line
print(d[[1]]$Na.me1, d[[1]]$Na.me2)
The data in d[[1]]$Na.me1 and d[[1]]$Na.me2 are both integers
[[1]]
Na.me1 Na.me2 Na.me3
10004563 382930 06042018
10005637 326289 04052018
I am aiming for a 4th column that looks like "10004563 382930"
We can use lapply to loop through the list and then create the column based on the description
lapply(d, transform, newcol = paste(Na.me1, Na.me2))
Or with tidyverse
library(tidyverse)
map(d, ~ .x %>%
unite(newcol, Na.me1, Na.me2, sep=" ", remove = FALSE))

how can I rbind data sets which have sequence name? [duplicate]

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)

Converting a data.frame to a list of data.frames by an aggregating value [duplicate]

This question already has answers here:
Split data.frame based on levels of a factor into new data.frames
(3 answers)
Closed 8 years ago.
I have this data.frame:
df = data.frame(chr=c("chr1","chr1","chr1"),start=c(1,2,3),end=c(11,12,13),strand=c("+","+","-"),tid=c("t1","t1","t2"),exon_id=c(1,2,1))
I'm looking for a function that would convert df to a list of data.frames where the aggregating value is df$tid.
Without a function, for this toy example, it would be:
df.list = list(df[1:2,], df[3,])
Since df$tid[1:2] is t1.
You are looking for split:
split(df, df$tid)
should do the trick.

Resources