0

I am trying to merge wider these 3 df. But this time, I am not getting it at all.

In the paste I had issues with merging wider, I believe due to package incompatibilities.

Here some partial attempts:

merged_df <- inner_join(dat_hr_test, dat_sbp_test )



dat_wide <- dat_sbp_test %>%
  merge(dat_hr_test,
        by=c("id2", "game_part" ))


merged_df <- merge(dat_hr_test, dat_sbp_test,  by=c(  "id2", "game_part"  ), all=FALSE)

The code runs but I always end with this type of result:

enter image description here

Thoughts?

dfs below:

dat_hr_test <- structure(list(id2 = c(6, 7, 9, 10, 11, 14, 15, 16), game_part = c("hr_p1_session16", 
"hr_p1_session16", "hr_p1_session16", "hr_p1_session16", "hr_p1_session16", 
"hr_p1_session16", "hr_p1_session16", "hr_p1_session16"), hr = c(144, 
126, 111, 133, 149, 109, 150, 133)), row.names = c(NA, -8L), class = c("tbl_df", 
"tbl", "data.frame"))



dat_sbp_test <- structure(list(id2 = c(6, 7, 9, 10, 11, 14, 15, 16), game_part = c("sbp_p1_session16", 
"sbp_p1_session16", "sbp_p1_session16", "sbp_p1_session16", "sbp_p1_session16", 
"sbp_p1_session16", "sbp_p1_session16", "sbp_p1_session16"), 
    sbp = c(142, 141, 156, 116, 100, 161, 122, 140)), row.names = c(NA, 
-8L), class = c("tbl_df", "tbl", "data.frame"))
^

dat_dbp_test <- structure(list(id2 = c(6, 7, 9, 10, 11, 14, 15, 16), game_part = c("dbp_p1_session16", 
"dbp_p1_session16", "dbp_p1_session16", "dbp_p1_session16", "dbp_p1_session16", 
"dbp_p1_session16", "dbp_p1_session16", "dbp_p1_session16"), 
    dbp = c(90, 90, 107, 96, 107, 92, 95, 85)), row.names = c(NA, 
-8L), class = c("tbl_df", "tbl", "data.frame"))
jtjtjtjt
  • 105
  • 6
  • 1
    The issue is that are no matches for the `game_part` columns of your datasets. Perhaps you could post an example of how your desired output should look like. – stefan Feb 03 '23 at 09:41
  • 1
    you can remove hp_ from game_part column in dat_hp_test data, similarly for other 2 data - then your join will work – Hemant Rupani Feb 03 '23 at 10:03
  • What a fail! I did not noticed the first words of that column were different. I think I will be able to fix this then. Thanks – jtjtjtjt Feb 03 '23 at 10:03

1 Answers1

0

You probably want to merge with all = TRUE instead of all = FALSE, also when you talk about merging more than two data.frames, you probably want to use Reduce.

Reduce(f = function(x, y) merge(x, y, by = c("id2", "game_part"), all = TRUE), list(dat_dbp_test, dat_hr_test, dat_sbp_test))

results

   id2        game_part dbp  hr sbp
1    6 dbp_p1_session16  90  NA  NA
2    6  hr_p1_session16  NA 144  NA
3    6 sbp_p1_session16  NA  NA 142
4    7 dbp_p1_session16  90  NA  NA
5    7  hr_p1_session16  NA 126  NA
6    7 sbp_p1_session16  NA  NA 141
7    9 dbp_p1_session16 107  NA  NA
8    9  hr_p1_session16  NA 111  NA
9    9 sbp_p1_session16  NA  NA 156
10  10 dbp_p1_session16  96  NA  NA
11  10  hr_p1_session16  NA 133  NA
12  10 sbp_p1_session16  NA  NA 116
13  11 dbp_p1_session16 107  NA  NA
14  11  hr_p1_session16  NA 149  NA
15  11 sbp_p1_session16  NA  NA 100
16  14 dbp_p1_session16  92  NA  NA
17  14  hr_p1_session16  NA 109  NA
18  14 sbp_p1_session16  NA  NA 161
19  15 dbp_p1_session16  95  NA  NA
20  15  hr_p1_session16  NA 150  NA
21  15 sbp_p1_session16  NA  NA 122
22  16 dbp_p1_session16  85  NA  NA
23  16  hr_p1_session16  NA 133  NA
24  16 sbp_p1_session16  NA  NA 140
Merijn van Tilborg
  • 5,452
  • 1
  • 7
  • 22