Suppose A is all the data that should appear for all id and year, and B is the data that is actually presented. From the information in B, it seems that not all id appear in every issue, so I want to know which id did not provide information in those years. And denote it as NA. SO... there are two dataframe A and B following down,
id <- c(1, 1, 1, 2, 2, 2, 3, 3, 3)
year <- c(2000, 2001, 2002, 2000, 2001, 2002, 2000, 2001, 2002)
A <- data.frame(id, year); colnames(A) <- c("ID", "year")
#
id <- c(1, 1, 1, 2, 2, 3)
year <- c(2000, 2001, 2002, 2001, 2002, 2002)
B <- data.frame(id, year); colnames(B) <- c("ID", "year")
cbind(A, B)
If I trying to use merge function would be like...
merge(A, B, by = "ID", all = TRUE)
but what I expected my new dataframe is like...
id <- c(1, 1, 1, 2, 2, 2, 3, 3, 3)
year.x <- c(2000, 2001, 2002, 2000, 2001, 2002, 2000, 2001, 2002)
year.y <- c(2000, 2001, 2002, NA, 2001, 2002, NA, NA, 2002)
D <- data.frame(id, year.x, year.y); D
Thank you!