-1

I have 2 data frames which look like this:

y = data.frame(ID= c(1, 1, 2, 2, 3, 3), year = c("2007", "2008", "2007", "2008", "2007", "2008"), aprank = c(3, 4, 1, 2, 5, 8))

z = data.frame(ID = c(1, 1, 1, 1 2, 2, 2, 2, 3, 3, 3, 3), year = c("2006", "2007", "2008", "2009", "2006", "2007", 2008, 2009, 2006, 2007, 2008, 2009), freq = c(500, 600, 800, 900, 200, 150, 300, 500, 100, 150, 300, 301))

and I'm expecting an outcome something like this:

t = data.frame(ID = c(1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3), year = c("2006", "2007", "2008", "2009", "2006", "2007", 2008, 2009, 2006, 2007, 2008, 2009), aprank = c(NA, 3, 4, NA, NA, 1,2, NA, NA, 5, 8, NA), freq = c(500, 600, 800, 900, 200, 150, 300, 500, 100, 150, 300, 301))

How can I do that? Any help would be appreciated

Neuxis
  • 81
  • 4

1 Answers1

0

We can use a join on the 'ID', 'year' columns of 'z', 'y' and assign the 'aprank' to create the new column 'aprank' in 'z'

library(data.table)
setDT(z)[y, aprank := aprank, on = .(ID, year)]
akrun
  • 874,273
  • 37
  • 540
  • 662