-2

I have diff data frames

data frame A

bed bake sea tea
aa 232 charles 232
bb 456 james 456
bbb 456 becky 456
ccaa 456 brown 456

Data frame B

BEARD bay pass butter
james 232 232 bb
brat 456 456 ccaa
Joyce 456 456 aabb
Mayer 456 456 dd

I have many of data frame A and data frame B in a folder what command should I run to re-arrange them base on data frame A header and row bind them as one

basically what I want the column to re-arrange themselves using the data frame A header as the base

I want to have something like this

bed bake sea tea
aa 232 charles 232
bb 456 james 456
bbb 456 becky 456
ccaa 456 brown 456
bb 232 james 232
ccaa 456 brat 456
aabb 456 Joyce 456
dd 456 Mayer 456
Yomi.blaze93
  • 401
  • 3
  • 10
  • change the colnames in Data frame B so they match where you want to join them on Data frame A, then use something like `data.table::rbindlist(..., use.names = TRUE)` for tyhe rowbinding – Wimpel Nov 29 '21 at 18:43
  • can you just change the column names of dataframe B? – neuron Nov 29 '21 at 19:09
  • I have many type of data frame B in a folder how do I change the name for each – Yomi.blaze93 Nov 29 '21 at 19:10
  • Have you had a look at related questions: https://stackoverflow.com/questions/6081439/changing-column-names-of-a-data-frame, https://stackoverflow.com/questions/3402371/combine-two-data-frames-by-rows-rbind-when-they-have-different-sets-of-columns – Peter Nov 29 '21 at 19:15
  • it doesnt answer my question – Yomi.blaze93 Nov 30 '21 at 09:48

1 Answers1

0

You can do this by using merge and by you will have to match up the columns though

df1:

df1<-read.table (text="bed  bake    sea tea
aa  232 charles 232
bb  456 james   456
bbb 456 becky   456
ccaa    456 brown   456", header=TRUE)

df2:

df2<-read.table (text="BEARD    bay pass    butter
james   232 232 bb
brat    456 456 ccaa
Joyce   456 456 aabb
Mayer   456 456 dd", header=TRUE)

merge data:

df3 <- merge(df1, df2, by.x=c("bed", "bake", "sea", "tea"), 
             by.y=c("butter", "bay", "BEARD", "pass"),all = TRUE)

If all your data frame A's and all your data frame B's have the same column order, you can rearrange dataframe B's columns using the follow. NOTE: it is unclear what order bay and passshould be in because they contain the same data.

dfB <- dfB[c(4,2,1,3)]

After you rearrange You can change the column names of data frame B using the following

colnames(dfB) <- c("bed", "baked","sea","tea")

and then just do a simple rbind() after

dfAB <- rbind(dfA,dfB)
neuron
  • 1,949
  • 1
  • 15
  • 30
  • what of if I have this data in a folder with diff data frames but the headers are different like the sample I gave, for example we have many of Data frame A and many of Data frame B in a folder and I'm trying to arrange them base on data frame A and row bind all – Yomi.blaze93 Dec 01 '21 at 13:37
  • @user16087142 You have to have some kind of knowledge of the structure of your data frames. If all of data frame B's are randomly ordered columns then there isn't much you can do – neuron Dec 01 '21 at 13:56
  • what im saying is that i have many of Data frame A in a folder with the same structure and I also have many of Data frame B in a folder with the same data frame B structure I'm looking for how to restructure them to align Data frame A structure – Yomi.blaze93 Dec 01 '21 at 14:00
  • You can just rearrange the columns before hand using `dfB <- dfB[c(4,2,1,3)]`, change the columns names of dfB using `colnames(dfB) <- c("bed", "baked","sea","tea")`, and then just rbind the two with `dfAB <- rbind(dfA,dfB)` – neuron Dec 01 '21 at 14:13
  • @user16087142 just updated my question for you – neuron Dec 01 '21 at 14:17
  • Thanks for your contribution, but case when all data frame names differ and you don't wanna keep open the data frame one by one to see which one is align with data frame A or which does not. I'm saying is there no script like Case_when any of the data frame did not match the data frame A, then rearrange to suit that frame Based on defined parameters I'm just saying or thinking thanks for the ideas and contribution so far – Yomi.blaze93 Dec 01 '21 at 14:49