【问题标题】:Filling in columns with certain columns from two dataframes in R用 R 中两个数据帧中的某些列填充列
【发布时间】:2021-04-28 21:08:31
【问题描述】:

我有两个数据框(例如:df1、df2)。我想填写从 df1 到 df2 的一列(测试),条件是两个数据帧之间具有相同的 ID 和名称。我曾尝试使用 for 循环,但发现很难实现。结果应该是这样的形式:“输出”。我会很感激你的帮助。谢谢!

Code:
var_of_interest <- c("testing")
df2[var_of_interest] <- lapply(var_of_interest, function(x), df1[[x]][match(df2$ID, df1$ID) & match(df2$Name, df1$Name)])

df1: df1 is a subset of df2(It has information on testing)
ID Name testing
1  a    100
2  a    90
3  a    80
4  a    70
5  a    60

df2:
ID Name testing
1  a    NA
2  a    NA
2  b    400
3  a    NA
3  c    300
4  a    NA
4  d    200
5  a    NA
5  e    150


output:
ID Name testing
1  a    100
2  a    90
2  b    400
3  a    80
3  c    300
4  a    70
4  d    200
5  a    60
5  e    150

【问题讨论】:

    标签: r lapply


    【解决方案1】:

    两个可能对你有用的选项

    基本 R 方法

    output <- merge(df1, df2, by = c('ID', 'Name'), all = TRUE)
    output$testing <- ifelse(is.na(output$testing.x), output$testing.y, output$testing.x)
    output <- subset(output, select = c(ID, Name, testing))
    

    或dplyr 方法

    library(dplyr)
    
    df2 %>% 
      left_join(df1, by = c('ID', 'Name')) %>%
      mutate(testing = coalesce(testing.x, testing.y)) %>%
      select(ID, Name, testing)
    

    编辑:根据评论,处理更多变量的不同方法。这种方法利用了dplyr 和tidyr。基本思想是将数据格式从“宽”(29 列)更改为“长”(ID、名称、var、value_df1 和 value_df2),这样更容易替换值。

    我没有在更大的假数据集上进行测试,但我认为这种方法应该适用于任意数量的变量。

    library(dplyr)
    library(tidyr)
    
    df2 %>% 
      left_join(df1, by = c('ID', 'Name')) %>%
      pivot_longer(-c('ID', 'Name'),
                   names_sep = '\\.',
                   names_to = c('var', 'source')) %>%
      pivot_wider(names_from = source, values_from = value) %>%
      group_by(ID, Name, var) %>%
      mutate(value = coalesce(x, y)) %>%
      select(-x, -y) %>%
      pivot_wider(names_from = var, values_from = value)
    

    【讨论】:

    • 嗨@nniloc。我能够使用基本的 R 方法让它工作。但是,如果我的 df1 和 df2 的列数比上面的示例多,有没有办法让输出具有相同的列数。例如,df1 有 29 个变量/列,df2 有 29 个变量和列。如何获得输出以获取 29 个变量/列而不是 57 个变量/列(假设按 ID 和名称)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-24
    • 2022-01-19
    • 1970-01-01
    相关资源
    最近更新 更多