title: R replace observations with data from another dataset
body: <p>I apologize in advance for the verboseness. </p>
  
  <p>My dataset contains MID dyads (1800-2001) for example</p>
  
  <pre><code>id   cc1 year cap1
  4114 2   1994  .
  4113 2   1994  .
  4113 2   1996  . 
  </code></pre>
  
  <p>The other dataset has cap1 score for each county-code year (from 1800-2001) for example </p>
  
  <pre><code>cc1 year CINC
  2   1816 0.039
  2   1817 0.035
  2   1818 0.036
  </code></pre>
  
  <p>I would like to use the CINC values from the second dataset to fill in the missing values in the cap1 variable in my data set. </p>
  
  <p>To be clear, I should also add the problem is that can be more than 1 or no MIDs per initiators year. So for example, my data set would have 2680 observations for the cap1 variable. However, the second dataset has 14199 observations. </p>
  
  <p>I search the forum on stackoverflow and consulted with a friend and came up with the following </p>
  
  <pre><code>mydata$cap1=mydata1[mydata1$ccode==mydata$cc1 &amp; mydata1$year==mydata$year,]$cinc
  </code></pre>
  
  <p>Where my mydata is my dataset and mydata1 is the second dataset</p>
  
  <p>This error was returned </p>
  
  <blockquote>
    <p>Error in <code>$&lt;-.data.frame</code>(<code>*tmp*</code>, "cap1", value = c(0.0396975, 0.0358166,  : 
      replacement has 3 rows, data has 2680</p>
    
    <p>In addition: Warning messages:</p>
    
    <p>1: In mydata1$ccode == mydata$cc1 :
      longer object length is not a multiple of shorter object length</p>
    
    <p>2: In mydata1$year == mydata$year :
      longer object length is not a multiple of shorter object length</p>
  </blockquote>
  
  <p>edit</p>
  
  <hr>
  
  <p>My dataset</p>
  
  <pre><code>    structure(list(idnum = c(4054L, 4186L, 4206L, 4273L, 2589L, 2587L
  ), cc1 = c(365L, 2L, 640L, 2L, 541L, 630L), cap1 = c(NA_real_, 
  NA_real_, NA_real_, NA_real_, NA_real_, NA_real_)), .Names = c("idnum", 
  "cc1", "cap1"), datalabel = "", time.stamp = "29 Nov 2015 23:36", formats = c("%8.0g", 
  "%8.0g", "%9.0g"), types = c(65529L, 65529L, 65527L), val.labels = c("", 
  "", ""), var.labels = c("MID ID number", "Initiator country code", 
  "Initiator's capabilities"), version = 117L, label.table = list(), expansion.fields = list(), strl = list(), byteorder = "LSF", row.names = 2675:2680, class = "data.frame")
  </code></pre>
  
  <p>Second dataset</p>
  
  <pre><code>structure(list(stateabb = structure(c(208L, 208L, 208L, 208L, 
  208L, 208L), .Label = c("AAB", "AFG", "ALB", "ALG", "AND", "ANG", 
  "ARG", "ARM", "AUH", "AUL", "AUS", "AZE", "BAD", "BAH", "BAR", 
  "BAV", "BEL", "BEN", "BFO", "BHM", "BHU", "BLR", "BLZ", "BNG", 
  "BOL", "BOS", "BOT", "BRA", "BRU", "BUI", "BUL", "CAM", "CAN", 
  "CAO", "CAP", "CDI", "CEN", "CHA", "CHL", "CHN", "COL", "COM", 
  "CON", "COS", "CRO", "CUB", "CYP", "CZE", "CZR", "DEN", "DJI", 
  "DMA", "DOM", "DRC", "DRV", "ECU", "EGY", "EQG", "ERI", "EST", 
  "ETH", "ETM", "FIJ", "FIN", "FRN", "FSM", "GAB", "GAM", "GDR", 
  "GFR", "GHA", "GMY", "GNB", "GRC", "GRG", "GRN", "GUA", "GUI", 
  "GUY", "HAI", "HAN", "HON", "HSE", "HSG", "HUN", "ICE", "IND", 
  "INS", "IRE", "IRN", "IRQ", "ISR", "ITA", "JAM", "JOR", "JPN", 
  "KEN", "KIR", "KOR", "KUW", "KYR", "KZK", "LAO", "LAT", "LBR", 
  "LEB", "LES", "LIB", "LIE", "LIT", "LUX", "MAA", "MAC", "MAD", 
  "MAG", "MAL", "MAS", "MAW", "MEC", "MEX", "MLD", "MLI", "MLT", 
  "MNC", "MNG", "MOD", "MON", "MOR", "MSI", "MYA", "MZM", "NAM", 
  "NAU", "NEP", "NEW", "NIC", "NIG", "NIR", "NOR", "NTH", "OMA", 
  "PAK", "PAL", "PAN", "PAP", "PAR", "PER", "PHI", "PMA", "PNG", 
  "POL", "POR", "PRK", "QAT", "ROK", "ROM", "RUS", "RVN", "RWA", 
  "SAF", "SAL", "SAU", "SAX", "SEN", "SEY", "SIC", "SIE", "SIN", 
  "SKN", "SLO", "SLU", "SLV", "SNM", "SOL", "SOM", "SPN", "SRI", 
  "STP", "SUD", "SUR", "SVG", "SWA", "SWD", "SWZ", "SYR", "TAJ", 
  "TAW", "TAZ", "THI", "TKM", "TOG", "TON", "TRI", "TUN", "TUR", 
  "TUS", "TUV", "UAE", "UGA", "UKG", "UKR", "URU", "USA", "UZB", 
  "VAN", "VEN", "WRT", "WSM", "YAR", "YEM", "YPR", "YUG", "ZAM", 
  "ZAN", "ZIM"), class = "factor"), ccode = c(990L, 990L, 990L, 
  990L, 990L, 990L), year = 2002:2007, irst = c(0L, 0L, 0L, 0L, 
  0L, 0L), milex = c(0L, 0L, 0L, 0L, 0L, 0L), milper = c(0L, 0L, 
  0L, 0L, 0L, 0L), pec = c(47.0876, 44.54492, 42.7761, 43.50082, 
  44.62415, 44.93124), tpop = c(178L, 180L, 182L, 183L, 185L, 187L
  ), upop = c(0L, 0L, 0L, 0L, 0L, 0L), cinc = c(5.12e-06, 5.08e-06, 
  5.05e-06, 5.01e-06, 5.01e-06, 4.99e-06), version = c(4L, 4L, 
  4L, 4L, 4L, 4L)), .Names = c("stateabb", "ccode", "year", "irst", 
  "milex", "milper", "pec", "tpop", "upop", "cinc", "version"), row.names = 14194:14199, class = "data.frame")
  </code></pre>
  
  <p>What I have attempted</p>
  
  <pre><code>require(foriegn)
  require(readstata13)
  mydata=read.dta13("Schultz-Geddes.dta")
  mydata1=read.csv("NMC_v4_0-3.csv")
  mydata$cap1=mydata1[mydata1$ccode==mydata$cc1 &amp; mydata1$year==mydata$year,]$cinc
  install.packages("dplyr")
  library(dplyr)
  joined &lt;- mydata %&gt;% 
    left_join(mydata1, c("cc1", "year"))
  </code></pre>