0

My goal is to combine multiple dataframes with unequal numbers of columns and rows without having to explicitly call each dataframe. The dataframe names all start with the same word, so I know there should be a way to do this.

Consider the following three dataframes (grp2, grp3 and grp4):

val1=c(.3, .3)
val2=c(.3, .3)
grp2 = data.frame (val1, val2)

val3 = c(.3, .4, .3)
val4 = c(.3, .3, .3)
val5 = c(.3, .3, .3)
grp3 = data.frame (val3, val4, val5)

val6 = c(.8, .9, .8, .3)
val7 = c(.9, .9, .8, .3)
val8 = c(.8, .9, .8, .3)
val9 = c(.9, .9, .9, .4)
grp4 = data.frame (val6, val7, val8)

These dataframes have different numbers of rows AND columns. I would like to merge them into one stacked dataframe. I can do so using:

all = merge (grp2, grp3, by=0, all=TRUE)
all = merge (all, grp4, by=0, all=TRUE)
all = (all[,-grep("Row.names",colnames(all))])

But I'd prefer to find a way where I don't have to specify the dataframe names. My dataframes are not always named grp2/3/4. For example, sometimes the dataframes are grp2, grp5, and grp6. I tried using rbind.fill like so:

rbind.fill(grp2, grp3, grp4)

but that a) does not stack the dataframes one on top of the other and b) does not get around the issue of having to explicitly specify the grp names.

I also tried putting the dataframes into a list and indexing the list:

df_list = mget(ls(pattern = "grp"))
all= merge(df_list[1], df_list[2], by = 0, all = TRUE)
all= merge(test, df_list[3], by = 0, all = TRUE)

This is somewhat better because it works through by index instead of by dataframe name, but in my actual dataset the column names get really messed up (ex. they go from being 511 to grp5.511 or 723 to grp7.723. I just want them to be 511, 723, etc., but the renaming seems like it would be tricky to do). I thought about trying to loop through my dataframes based on name (ex. find all variables that start with grp and merge sequentially), but I was having trouble executing this in a loop. Any advice would be much appreciated!

EDIT In response to Parfait, here is what the output looks like when I use rbind.fill: enter image description here

And here is what I would like the output to look like: enter image description here

TAH
  • 120
  • 1
  • 3
  • 11
  • 1
    By *merge*, do you really mean *append*? This question can either be a duplicate of chain [merge](https://stackoverflow.com/questions/14096814/merging-a-lot-of-data-frames) or [append](https://stackoverflow.com/questions/16138693/rbind-multiple-data-sets). Why doesn't `rbind.fill` work? I do not understand *but that a) does not stack the dataframes one on top of the other*? – Parfait Feb 21 '20 at 20:51
  • Hi Parfait! I've updated my post to show what the rbind.fill output looks like and why it does not match what I'm hoping to get. – TAH Feb 21 '20 at 20:59
  • 1
    Run above chain merge by placing all dfs into a list: `Reduce(function(x,y) merge(x,y, by=0, all=TRUE), list(grp2, grp3, grp4))` – Parfait Feb 21 '20 at 21:05

0 Answers0