Skip to content
Prev 363021 / 398498 Next

Replace any values in a data frame based on another data frame

Your use of HTML email corrupted your example slightly, but I was able to 
fix it. Please follow the Posting Guide and set your emails to Plain Text 
mode when posting to this mailing list in the future.

Here is one way:

# you have to be careful about mucking with factors
# convert columns to factors after you have finished
# changing values in them
# start by not creating factors in the first place
df1 <- data.frame( col1 = 1:6
                  , col2 = c( rep( "a", 3 )
                            , rep( "b", 3 )
                            )
                  , col3 = c( rep( "AA", 2 )
                            , rep( "BB", 2 )
                            , rep( "CC", 2 )
                            )
                  , col4 = c( 1, 8, 6, 9, 7, 6 )
                  , stringsAsFactors = FALSE
                  )

df2 <- data.frame( col1 = c( 1, 3, 5, 6 )
                  , col2 = c( rep( "a", 2 )
                            , rep( "b", 2 )
                            )
                  , col3 = c( rep( "AA", 1 )
                            , rep( "EE", 1 )
                            , rep( "FF", 1 )
                            , rep( "CC", 1 )
                            )
                  , col4 = c( 1, 8, 5, 9 )
                  , stringsAsFactors = FALSE
                  )

df3 <- merge( df1, df2, by = c( "col1", "col2" ), all = TRUE )
idx <- !is.na( df3$col3.y )
df3$col3.x[ idx ] <- df3$col3.y[ idx ]
df3$col4.x[ idx ] <- df3$col4.y[ idx ]
df3$col3.y <- df3$col4.y <- NULL
names( df3 )[ 3:4 ] <- c( "col3", "col4" )
df3
On Sun, 31 Jul 2016, Marine Regis wrote:

            
---------------------------------------------------------------------------
Jeff Newmiller                        The     .....       .....  Go Live...
DCN:<jdnewmil at dcn.davis.ca.us>        Basics: ##.#.       ##.#.  Live Go...
                                       Live:   OO#.. Dead: OO#..  Playing
Research Engineer (Solar/Batteries            O.O#.       #.O#.  with
/Software/Embedded Controllers)               .OO#.       .OO#.  rocks...1k