Moving values ​​from one data frame to another, depending on the value of the variable

Not familiar with R, I have the following problem: I want to add values probepositionfrom a dataframe mlpato a dataframe patients, and the values probepositionare connected by the values ​​being present both in mlpaand patients(i.e., probeand patprobe). As I understand it, this problem is not covered by ordinary data management tutorials.

#mlpa:
probe <- c(12,15,18,19)
probeposition <- c(100,1200,500,900)
mlpa = data.frame(probe = probe, probeposition = probeposition)
#patients:
patid <- c('AT', 'GA', 'TT', 'AG', 'GG', 'TA')
patprobe <- c(12, 12, NA, NA, 18, 19)
patients = data.frame(patid = patid, patprobe = patprobe)

#And that what I finally want:
patprobeposition = c(100, 100, NA, NA, 500, 900)  
patients$patprobeposition = patprobeposition

Update

After Andri’s answer, I realized that I should mention that there are several “probes” in the patient data set, so in fact the data will be more similar (in fact, there will be not only probes1 and probe2, but probe1-probe4):

mlpa <- data.frame(probe = c(12,15,18,19),
                   probeposition = c(100,1200,500,900) ) 
patients <- data.frame(patid = c('AT', 'GA', 'TT', 'AG', 'GG', 'TA'),
                       probe1 = c(12, 12, NA, NA, 18, 19), 
                       probe2 = c(15, 15, NA, NA, 19, 19) )

And I want this:

patients <- data.frame(patid = c('AT', 'GA', 'TT', 'AG', 'GG', 'TA'),   
                       probe1 = c(12, 12, NA, NA, 18, 19), 
                       probe2 = c(15, 15, NA, NA, 19, 19), 
                       position1 = c(100, 100, NA, NA, 500, 900), 
                       position2 = c(1200, 1200, NA, NA, 900, 900)) 
+3
source share
2 answers

, merge, .

merge , - , , . , patprobe probe:

mlpa <- data.frame(
  probe = c(12,15,18,19),
  probeposition = c(100,1200,500,900)
)

patients <- data.frame(
  patid = c('AT', 'GA', 'TT', 'AG', 'GG', 'TA'),
  probe = c(12, 12, NA, NA, 18, 19)
)

merge. , merge ( ). patients ( ). , all.x=TRUE:

merge(patients, mlpa, all.x=TRUE, sort=FALSE)

  probe patid probeposition
1    12    AT           100
2    12    GA           100
3    18    GG           500
4    19    TA           900
5    NA    TT            NA
6    NA    AG            NA
+2

reshape2 :

require(reshape2)
m.patients = melt(patients)
m.patients = merge(m.patients, mlpa, 
                   by.x = "value", 
                   by.y = "probe", 
                   all = TRUE)
reshape(m.patients, direction="wide", 
        timevar="variable", idvar="patid")

, , , .

   patid value.probe1 probeposition.probe1 value.probe2 probeposition.probe2
1     AT           12                  100           15                 1200
2     GA           12                  100           15                 1200
5     GG           18                  500           19                  900
7     TA           19                  900           19                  900
9     TT           NA                   NA           NA                   NA
10    AG           NA                   NA           NA                   NA

Update

, reshape2, :

m.patients = melt(patients, id.vars="patid", variable_name="time")
m.patients = melt(merge(m.patients, mlpa, by.x = "value", 
                        by.y = "probe", all = TRUE))
dcast(m.patients, patid ~ variable + time )

:

  patid value_probe1 value_probe2 probeposition_probe1 probeposition_probe2
1    AG           NA           NA                   NA                   NA
2    AT           12           15                  100                 1200
3    GA           12           15                  100                 1200
4    GG           18           19                  500                  900
5    TA           19           19                  900                  900

2: Base R Reshape

reshape2.

patients.l = reshape(patients, direction="long", idvar="patid", 
                     varying=c("probe1", "probe2"), sep="")
reshape(merge(patients.l, mlpa, all = TRUE), direction="wide", 
        idvar="patid", timevar="time")

:

   patid probe.1 probeposition.1 probe.2 probeposition.2
1     AT      12             100      15            1200
2     GA      12             100      15            1200
5     GG      18             500      19             900
7     TA      19             900      19             900
9     TT      NA              NA      NA              NA
10    AG      NA              NA      NA              NA
0

All Articles