简体   繁体   English

R:将值从一个 dataframe 中的一列复制到另一个 dataframe 中的几个选定列,用于组

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

I would like to copy the value from a dataframe to another dataframe. The difficulty for me is that I have four groups in both dataframes and in the dataframe where I would like to copy the values, I have several columns where I want to insert the value.我想将值从 dataframe 复制到另一个 dataframe。对我来说,困难在于我在两个数据框中都有四个组,在 dataframe 中我想复制值,我有几列我想插入价值。 More specifically, the dataframe (df1) from which I would like to copy the values looks like this:更具体地说,我想从中复制值的 dataframe (df1) 如下所示:

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

The dataframe (df2) where I want to insert the values looks like this:我要插入值的 dataframe (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))

I would like to copy the data in df1 in column "Value" to df2 to columns 2 to 8 ("Rating 2017-06" to "Rating 2017-12") for each Name.我想将 df1 中的“值”列中的数据复制到 df2 中的每个名称的第 2 列到第 8 列(“Rating 2017-06”到“Rating 2017-12”)。

What I have tried so far:到目前为止我尝试了什么:

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

The problem is with this code that I cannot specifically copy the values form df1 to selected columns as mentioned above in df2.问题在于这段代码,我无法将 df1 中的值专门复制到上面提到的 df2 中的选定列。 I don't know what I have to right in the code that I would be able to do that.我不知道我必须在代码中做些什么才能做到这一点。

Could someone help me here?有人可以帮我吗? Thank you already.已经谢谢你了。

Try this approach:试试这个方法:

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

Created on 2022-04-30 by the reprex package (v2.0.1)reprex package (v2.0.1) 创建于 2022-04-30

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果多列匹配,R 从一个 dataframe 复制到另一个 - R copy from one dataframe to another if multiple columns match R:识别一列中的非 NA 值并创建 dataframe 并选择另一列中的值 - R: Identify non-NA values from one column and create dataframe with values from another column based rows selected 如何根据第三列的共享 ID 从一个数据框中获取数据并将其复制到另一个数据框中的现有列中 - How to take data from one dataframe and copy it into existing columns in another dataframe based on the shared ID of a third column 根据 R 中的另一个 dataframe 复制 dataframe 中的列 - Copy columns in a dataframe based on another dataframe in R 使用 R 中的特定条件将值从一个 dataframe 复制并重复到另一个 dataframe - Copy and repeat values ​from one dataframe to another dataframe with specific conditions in R 如何将一个 dataframe 中的列中的数字匹配到另一个 dataframe R 中的粗粒度值列 - How to match numbers in a column from one dataframe to a column of coarser grained values in another dataframe R R:从另一个 dataframe 中将值填充为所选列的平均值(步长 = 12) - R: fill values to columns as mean of selected columns (step = 12) from another dataframe 基于R中数据框中的单个列的几列的平均偏差 - Average deviation from several columns based on a single column in a dataframe in R 将一个 dataframe 中的值加到另一个 R 中 - Summing values from one dataframe to another in R 从 R dataframe 中的特定行将数据从一列复制到另一列 - Copy data from one column to another column from a specific row in R dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM