R语言 将具有可变行数的DF添加到具有静态行数的DF

ldxq2e6h  于 2023-01-03  发布在  其他
关注(0)|答案(1)|浏览(114)

我有一个25行的数据框,如下所示

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
))

我还有另一个 Dataframe ,当列opp〉0时,它可能包含1-25行。例如:

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"))

我想要的是当第二个数据集中有 Dataframe 时,将它们合并成一个 Dataframe ,如果没有,则应该使用hc_y和hc_x的分组变量来维护第一个 Dataframe 中的信息。
所以最终的结果应该是25行,怎么做呢?

gab6jxml

gab6jxml1#

我将 Dataframe 命名为df1和df2。

left_join(df1,df2)

你是这个意思吗?我不明白你的问题

相关问题