0

Sorry for this very basic question: I have an empty data frame with 500+ columns where selected data from another data frame has to be copied in.

Here is my example:

dest<-read.table(text='PID  DEMOGID GENDER AGEINYEARS LVSF RVSF', header=TRUE)

source<-read.table(text = '
     INTERNALID SEX 
1     147    "M" 
2     150    "F" 
3     144    "M" 
4     149    "F" ', header = TRUE)

After copying the data, my dest data frame should look like this:

dest
PID        DEMOGID    GENDER     AGEINYEARS LVSF       RVSF   
147        NA         M          NA         NA         NA
150        NA         F          NA         NA         NA
144        NA         M          NA         NA         NA
149        NA         M          NA         NA         NA

Thanks in advance.

Johann Horvat
  • 1,285
  • 1
  • 14
  • 18

2 Answers2

2

You can merge the two dataframes.

merge(dest, source, by.y = c('INTERNALID', 'SEX'), 
                    by.x = c('PID', 'GENDER'), all.y = TRUE)

#  PID GENDER DEMOGID AGEINYEARS LVSF RVSF
#1 144      M      NA         NA   NA   NA
#2 147      M      NA         NA   NA   NA
#3 149      F      NA         NA   NA   NA
#4 150      F      NA         NA   NA   NA
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • I duped with cbind.fill but now that I see, isn't this a dupe of the classic merge? – Sotos May 19 '20 at 07:05
  • 1
    I think the question is not really a dupe (although looking at answer we may feel that :P) as there could be multiple approaches to solve it. `merge` is one way but I think this can also be solved with `cbind` and likes. – Ronak Shah May 19 '20 at 07:10
2

Looks like you could just rename the columns in dest. Or you could do a join/merge, for example, using the dplyr package as follows:

dest <- read.table(text='PID  DEMOGID GENDER AGEINYEARS LVSF RVSF', header=TRUE,
                   colClasses = c(PID = "integer", GENDER = "character"))

source<-read.table(text = '
     INTERNALID SEX 
1     147    "M" 
2     150    "F" 
3     144    "M" 
4     149    "F" ', header = TRUE)

library("dplyr")

right_join(dest, source, by = c(PID = "INTERNALID", GENDER = "SEX"))
#>   PID DEMOGID GENDER AGEINYEARS LVSF RVSF
#> 1 147      NA      M         NA   NA   NA
#> 2 150      NA      F         NA   NA   NA
#> 3 144      NA      M         NA   NA   NA
#> 4 149      NA      F         NA   NA   NA

Notice that dplyr requires the to-be merged columns to be of the same type. That's why I used colClasses for reading in dest.

hplieninger
  • 3,214
  • 27
  • 32