Home > OS >  How do I use the suffixes argument when merging multiple dataframes in R?
How do I use the suffixes argument when merging multiple dataframes in R?

Time:12-19

First merge of two datasets:

Agg_Bpm <- merge(agg_bpm_mean, agg_bpm_median, by = "Group.1", all = TRUE, suffixes = c(".mean",".median"))

Output:

      Group.1   x.mean x.median
1  2022484408 80.23686       76
2  2026352035 93.77631       95
3  2347167796 76.72279       73
4  4020332650 82.30058       83
5  4388161847 66.13300       62

Now I want to merge another dataset:

> agg_bpm_max
      Group.1   x
1  2022484408 203
2  2026352035 125
3  2347167796 195
4  4020332650 191
5  4388161847 180

So I try this:

Agg_Bpm <- merge(Agg_Bpm, agg_bpm_max, by = "Group.1", all = TRUE, suffixes = c(c(".mean",".median"),".max"))

Can merge it but how do I suffix the new column properly??

> Agg_Bpm
      Group.1   x.mean x.median   x
1  2022484408 80.23686       76 203
2  2026352035 93.77631       95 125
3  2347167796 76.72279       73 195
4  4020332650 82.30058       83 191
5  4388161847 66.13300       62 180

CodePudding user response:

Could you try:

Agg_Bpm <- merge(Agg_Bpm, agg_bpm_max, by = "Group.1", all = TRUE, suffixes = c("",".max"))

Pls provide next time a reproducible dataset. You can do this with function dput(Agg_Bpm)

CodePudding user response:

I decided a simple solution was to just not use the suffixes argument and just rename columns after the fact:

> names(Agg_Bpm) <- c("id","mean","median","max","sd")
> Agg_Bpm
           id     mean median max       sd
1  2022484408 80.23686     76 203 17.59559
2  2026352035 93.77631     95 125 12.61053
3  2347167796 76.72279     73 195 15.51571
4  4020332650 82.30058     83 191 15.91159
5  4388161847 66.13300     62 180 15.84728
  • Related