0

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.

1
  • 2
    Welcome to stackoverflow. It looks as if you want to combine table_p and table_f by the Date variable. If that is the case then dplyr::left_join is your friend. If you could include your data as dataframe objects that would help others answer your question: use dput(table_p) and dput(table_f) to do this. Commented Jun 8, 2021 at 18:40

1 Answer 1

1

Try this:

library(dplyr)


  table_p %>% 
  left_join(table_f)
#> Joining, by = "date"
#>                  date bin1 bin2 flow    tur
#> 1 2021-03-02 19:30:00   12    4    1 0.0243
#> 2 2021-03-02 19:45:00   11    6    1 0.0872
#> 3 2021-03-02 20:00:00   12    3    0 0.0654

data

table_p <- 
  data.frame(date = c("2021-03-02 19:30:00", "2021-03-02 19:45:00", "2021-03-02 20:00:00"),
             bin1 = c(12, 11, 12),
             bin2 = c(4, 6 , 3))


table_f <- 
  data.frame(date = c("2021-03-02 19:30:00", "2021-03-02 19:45:00", "2021-03-02 20:00:00"),
             flow = c(1, 1, 0),
             tur = c(0.0243, 0.0872, 0.0654))

Created on 2021-06-08 by the reprex package (v2.0.0)

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the response, Peter! I would like to use left_join as it seems to work the best but since I have multiple data frames within each list I need a way to iterate through them so I can use left_join once they match. Do you know of a way for me to incorporate matching the dataframe names in an if statement before I perform left_join?
Without a clear understanding of your data frames and what you mean by a list I am not sure how best to respond. Perhaps you may need to combine all the p data frames and f data frames into two dfs adding a grouping variable so you know the origin data frame and then carryout your left join by df group and date. The only way to be sure is if you include a minimum reproducible example of your data pasted into the question so it can be copied to test solutions. Have a look at minimal reproducible example

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.