Having troubles joining dataframe's

Goal

I have multiple dataframes with two columns in common station and events. I know this is pretty basic stuff but for some reason, I cannot seem to join them in a way that doesn't create new rows. I tried all variations of join() but either new rows were added or data was omitted. Any help on where I am going wrong would be much appreciated.

Data

wave_ps_coda <- read_delim(file = "https://gitlab.com/Bryanrt-geophys/ml-seismic-application/-/raw/master/R_wrangle_data/wave_ps_coda.csv", delim = ",", col_names = T)
wave_data <- read_delim(file = 'https://gitlab.com/Bryanrt-geophys/ml-seismic-application/-/raw/master/R_wrangle_data/wave_data.csv', delim = ",", col_names = T)
hypo_data <- read_delim(file = 'https://gitlab.com/Bryanrt-geophys/ml-seismic-application/-/raw/master/R_wrangle_data/hypo71_data.csv', delim = ",", col_names = T)

Failed Attempts

left_join(wave_ps_coda, hypo_data)
right_join(wave_ps_coda, hypo_data)
inner_join(wave_ps_coda, hypo_data)
full_join(wave_ps_coda, hypo_data)

From there, I imagine I could %>% ..._join(, wave_data) to bring in the third dataframe. Am I going in the right direction?

An errant leading space in the station variable. (Won't reprex properly, so just the code)

suppressPackageStartupMessages({
  library(dplyr)
  library(magrittr)
  library(stringr)
})


wave_ps_coda <- read_delim(file = "https://gitlab.com/Bryanrt-geophys/ml-seismic-application/-/raw/master/R_wrangle_data/wave_ps_coda.csv", delim = ",", col_names = T)

wave_data <- read_delim(file = 'https://gitlab.com/Bryanrt-geophys/ml-seismic-application/-/raw/master/R_wrangle_data/wave_data.csv', delim = ",", col_names = T)
hypo_data <- read_delim(file = 'https://gitlab.com/Bryanrt-geophys/ml-seismic-application/-/raw/master/R_wrangle_data/hypo71_data.csv', delim = ",", col_names = T)

# trim leading blanks

wave_data %<>% mutate(station = str_trim(station,"left"),
                     phase = str_trim(phase,"left"))

hypo_data %<>% mutate(station = str_trim(station,"left"))
                     
inner_join(inner_join(wave_ps_coda, wave_data),hypo_data)        

Thank you kindly @technocrat, once again you taught me something great.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.