Using one data.frame to update another

后端 未结 7 421

Given 2 data frames that are identical in terms of column names/datatypes, where some columns uniquely identify the rows, is there an efficient function/method for one data.

相关标签:
7条回答
  • 2020-12-20 12:54

    I'd use data.table objects. This code seems to work on your example:

    library(data.table)
    
    # set keys
    original.dt <- data.table(original, key=c("Name", "Id"))        
    replacement.dt <- data.table(replacement, key=c("Name", "Id"))
    
    goal2 <- original.dt
    # subset and reassign
    # goal2[replacement.dt[, list(Name, Id)]] <- replacement.dt
    goal2[replacement.dt] <- replacement.dt  # cleaner and faster, see Matthew's comment
    
    goal2 <- as.data.frame(goal2)
    
    identical(goal, goal2) # FALSE, why? See Joris's comment
    all.equal(goal, goal2) # TRUE
    
    0 讨论(0)
提交回复
热议问题