I need to match data frames that are in two separate lists, one for particle data and one for flow data, based on their name. Each data frame name starts with Filter 11, Filter 12, Filter 13, etc. so I would like to match the data frames from both lists that have the same filter number and combine them based on their date. A simplified example of one data frame from each list is as shown:
Filter 12
|Date | Bin 1 | Bin 2 |
|--------------------|-----------------|
|2021-03-02 19:30:00 | 12 | 4 |
|2021-03-02 19:45:00 | 11 | 6 |
|2021-03-02 20:00:00 | 12 | 3 |
Filter 12 Flow
|Date | Flow | Turbidity |
|--------------------|--------|-----------|
|2021-03-02 19:30:00 | 1 | 0.0243 |
|2021-03-02 19:45:00 | 1 | 0.0872 |
|2021-03-02 20:00:00 | 0 | 0.0654 |
I am attempting to use an embedded for loop to accomplish this but I am not sure how to compare the file names in the if statement. the two lists are table_p and table_f. This is the code I have so far
for (p_file in table_p) {
for (f_file in table_f) {
if (nchar(p_file[8:9]) == nchar(f_file[8:9])) {
final_data_join <- left_join(p_file , f_file, by = c("Date"))
}
}
}
I don't think that nchar is the correct function to use to compare them but I am not sure if there is one specifically for this. I would like my final output to look as such:
Filter 12
| Date | Bin 1 | Bin 2 | Flow | Turbidity |
|--------------------|-------|-------|------|-----------|
|2021-03-02 19:30:00 | 12 | 4 | 1 | 0.0243 |
|2021-03-02 19:45:00 | 11 | 6 | 1 | 0.0872 |
|2021-03-02 20:00:00 | 12 | 3 | 0 | 0.0654 |
I am new to r and to the forum so please let me know if I have not given enough information.
Datevariable. If that is the case thendplyr::left_joinis your friend. If you could include your data as dataframe objects that would help others answer your question: usedput(table_p)anddput(table_f)to do this.