0

Add rows to dataframe with 2 set columns and 100 other columns to 0 or NA depending on the column type. 0 for numeric columns and NA for character columns.

Original Dataframe:

 date   name   score1 score2 score3 last_name
 1/1/16 Bill      3     3       3    Johnson              
 1/1/16 Angela    3     3       3    Harris
 1/1/16 Bob       3     3       3    Johnson                                       
 1/2/16 Bill      3     3       3    Johnson                                        
 1/2/16 Angela    3     3       3    Harris    

The second Dataframe:

 date     name  
1/3/16   George
1/3/16   Harold

Desired Dataframe

 date   name   score1 score2 score3 last_name
 1/1/16 Bill      3     3       3    Johnson              
 1/1/16 Angela    3     3       3    Harris
 1/1/16 Bob       3     3       3    Johnson                                       
 1/2/16 Bill      3     3       3    Johnson                                        
 1/2/16 Angela    3     3       3    Harris  
 1/3/16 George    0     0       0    NA               
 1/3/16 Harold    0     0       0    NA                                    
user6452857
  • 117
  • 2
  • 9
  • 1
    @Sotos I get NA instead of 0's for the expected output which can be replaced `merge(df1, df2, by = c('date', 'name'), all=TRUE)` – akrun Feb 10 '17 at 11:04
  • 1
    ahh, ok. Didn't catch that – Sotos Feb 10 '17 at 11:04
  • 1
    Naming the first data frame a and the second b, the following should do the trick: library(dplyr) `d <- bind_rows(a,b)` `d[sapply(d, is.numeric)][is.na(d[sapply(d, is.numeric)])] <- 0` – Wolfgang Feb 10 '17 at 11:21
  • it is more a duplicate of: http://stackoverflow.com/questions/19379081/how-to-replace-na-values-in-a-table-for-selected-columns-data-frame-data-tab – Wolfgang Feb 10 '17 at 11:24

0 Answers0