R: Updating a data frame with another data frame

后端 未结 4 1137
别那么骄傲
别那么骄傲 2020-12-18 12:51

Let\'s say our initial data frame looks like this:

df1 = data.frame(Index=c(1:6),A=c(1:6),B=c(1,2,3,NA,NA,NA),C=c(1,2,3,NA,NA,NA))

> df1
  Index A  B  C
         


        
4条回答
  •  半阙折子戏
    2020-12-18 12:58

    Not sure what the general case or conditions would be, but this works for this instance without dplyr

    df3 <- as.matrix(df1)
    df3[which(is.na(df3))] <- as.matrix(df2)
    df3 <- as.data.frame(df3)
    df3
    
      A B C
    1 1 1 1
    2 2 2 2
    3 3 3 3
    4 4 4 5
    5 5 4 5
    6 6 4 5
    

提交回复
热议问题