Home > database >  Adding a DF with a variable number of rows to one with a static number
Adding a DF with a variable number of rows to one with a static number

Time:12-31

I have a Dataframe that is 25 rows, seen below

structure(list(hc_y = c(250, 250, 250, 250, 250, 275, 275, 275, 
275, 275, 300, 300, 300, 300, 300, 325, 325, 325, 325, 325, 350, 
350, 350, 350, 350), hc_x = c(-50, -25, 0, 25, 50, -50, -25, 
0, 25, 50, -50, -25, 0, 25, 50, -50, -25, 0, 25, 50, -50, -25, 
0, 25, 50), opp = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), oaa = c(NA, NA, NA, NA, NA, 
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 
NA, NA, NA, NA)), class = "data.frame", row.names = c(NA, -25L
))

And I have another dataframe that could have anywhere from 1-25 of those rows when the column opp > 0. For Example, this:

structure(list(hc_x = c(-50, -50, -50, -50, -25, -25, -25, -25, 
-25, 0, 0, 0, 0, 25, 25, 25, 25, 50, 50, 50, 50, 50), hc_y = c(250, 
300, 325, 350, 250, 275, 300, 325, 350, 250, 300, 325, 350, 250, 
275, 325, 350, 250, 275, 300, 325, 350), oaa = c(0.014, 0.084, 
0.053, -0.266, 0.281, 0.012, 0.018, 0.017, 0.109, -0.048, 0.017, 
0.166, 0.524, -0.544, 0.163, 0.036, 0.412, -0.005, 0.033, 0.061, 
1.726, 0.528), opp = c(2, 4, 2, 5, 8, 1, 1, 2, 2, 3, 2, 1, 3, 
4, 3, 1, 3, 1, 2, 3, 5, 9)), row.names = c(NA, -22L), groups = structure(list(
    hc_x = c(-50, -25, 0, 25, 50), .rows = structure(list(1:4, 
        5:9, 10:13, 14:17, 18:22), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), row.names = c(NA, -5L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

What I want is to combine these into one dataframe when it is present in the second data set. If it is not present it should maintain the information from the first one, using the grouping vars of hc_y an hc_x.

So the final product should always be 25 rows. How can this be done?

CodePudding user response:

I called the dataframes df1 and df2.

left_join(df1,df2)

Is that what you meant? I didnt understand your question

  • Related