how to use merge() to update a table in R

前端 未结 6 699
孤独总比滥情好
孤独总比滥情好 2020-11-27 20:47

I\'m trying to figure out how to use merge() to update a database.

Here is an example. Take for example the data frame foo



        
6条回答
  •  一生所求
    2020-11-27 21:22

    Another approach could be:

    1. Remove the NAs from the first data fram

    2. Use rbind to append the data instead of using merge:

    These are the original two data frames:

    foo <- data.frame(index=c('a', 'b', 'c', 'd'), value=c(100, 101, NA, NA))
    bar <- data.frame(index=c('c', 'd'), value=c(200, 201))
    

    (1) Use the negation of is.na to remove the NAs:

    foo_new <- foo[!is.na(foo$value),]
    

    (2) Bind the data frames and you'll get the answer you were looking for

    new_df <- rbind(foo_new,bar)
    
                new_df
                index value
                1     a   100
                2     b   101
                3     c   200
                4     d   201
    

提交回复
热议问题