R:将值从一个数据框中的列复制到另一个数据框中的多个选定列以进行分组

R: Copy values from a column in one dataframe to several selected columns in another dataframe for groups

我想将值从一个数据框复制到另一个数据框。对我来说,困难在于我在两个数据框中都有四个组,在我想复制值的数据框中,我有几列要插入值。更具体地说,我想从中复制值的数据框 (df1) 如下所示:

structure(list(Name = c("A", "B", "C", "D"), Value = c(2L, 5L, 
3L, 2L)), class = "data.frame", row.names = c(NA, -4L))

我要插入值的数据框 (df2) 如下所示:

structure(list(Name = c("A", "B", "C", "D"), `Rating 2017-06` = c(NA, 
NA, NA, NA), `Rating 2017-07` = c(NA, NA, NA, NA), `Ratin g 2017-08` = c(NA, 
NA, NA, NA), `Rating 2017-09` = c(NA, NA, NA, NA), `Rating 2017-10` = c(NA, 
NA, NA, NA), `Rating 2017-11` = c(NA, NA, NA, NA), `Rating 2017-12` = c(NA, 
NA, NA, NA), `Rating 2018-01` = c(4L, 4L, 3L, 3L), `Rating 2018-02` = c(3L, 
4L, 3L, 2L)), class = "data.frame", row.names = c(NA, -4L))

我想将 df1 中的“值”列中的数据复制到 df2 中的每个名称的第 2 到第 8 列(“Rating 2017-06”到“Rating 2017-12”)。

到目前为止我尝试过的:

Merged_Data <- df1 %>% 
  left_join(df1, df2, by="Name")

此代码存在问题,我无法将 df1 中的值专门复制到上面提到的 df2 中的选定列。我不知道我必须在代码中做些什么才能做到这一点。

有人可以帮我吗? 已经谢谢了。

试试这个方法:

library(tidyverse)

df1 <- structure(list(
  Name = c("A", "B", "C", "D"),
  Value = c(2L, 5L,
            3L, 2L)
),
class = "data.frame",
row.names = c(NA,-4L))

df2 <- structure(
  list(
    Name = c("A", "B", "C", "D"),
    `Rating 2016-06` = c(NA,
                         NA, NA, NA),
    `Rating 2017-07` = c(NA, NA, NA, NA),
    `Ratin g 2017-08` = c(NA,
                          NA, NA, NA),
    `Rating 2017-09` = c(NA, NA, NA, NA),
    `Rating 2017-10` = c(NA,
                         NA, NA, NA),
    `Rating 2017-11` = c(NA, NA, NA, NA),
    `Rating 2017-12` = c(NA,
                         NA, NA, NA),
    `Rating 2018-01` = c(4L, 4L, 3L, 3L),
    `Rating 2018-02` = c(3L,
                         4L, 3L, 2L)), class = "data.frame", row.names = c(NA, -4L))

df2 |> 
  left_join(df1) |> 
  mutate(across(contains("2016") | contains("2017"), ~ Value)) |> 
  select(- Value)
#> Joining, by = "Name"
#>   Name Rating 2016-06 Rating 2017-07 Ratin g 2017-08 Rating 2017-09
#> 1    A              2              2               2              2
#> 2    B              5              5               5              5
#> 3    C              3              3               3              3
#> 4    D              2              2               2              2
#>   Rating 2017-10 Rating 2017-11 Rating 2017-12 Rating 2018-01 Rating 2018-02
#> 1              2              2              2              4              3
#> 2              5              5              5              4              4
#> 3              3              3              3              3              3
#> 4              2              2              2              3              2

df2 |> 
  left_join(df1) |> 
  mutate(across(`Rating 2016-06`:`Rating 2017-12`, ~ Value)) |> 
  select(- Value)
#> Joining, by = "Name"
#>   Name Rating 2016-06 Rating 2017-07 Ratin g 2017-08 Rating 2017-09
#> 1    A              2              2               2              2
#> 2    B              5              5               5              5
#> 3    C              3              3               3              3
#> 4    D              2              2               2              2
#>   Rating 2017-10 Rating 2017-11 Rating 2017-12 Rating 2018-01 Rating 2018-02
#> 1              2              2              2              4              3
#> 2              5              5              5              4              4
#> 3              3              3              3              3              3
#> 4              2              2              2              3              2

reprex package (v2.0.1)

于 2022-04-30 创建