I want to construct two data frames and merge them without using any form of merge(). Instead I need to use set operations union() and match() or %in% operator. The following output must display the content of d1,d2 and the result of merging d1 and d2.
I have figured out how to do this with merge() but I cannot find out how to do it using union() and match() or %in% operator. Or any other way of doing this. Also my output doesn't match what the output should be. Im a beginner thanks for your help.
d1.Kids <- c("Jack", "Jill", "Jillian", "John", "James")
d1.States <- c("CA", "MA", "DE", "HI", "PA")
d1 <- data.frame(d1.Kids, d1.States, stringsAsFactors = FALSE)
d2.Ages <- c(10, 7, 12, 30)
d2.Kids <- c("Jill", "Jillian", "Jack", "Mary")
d2 <- data.frame(d2.Ages, d2.Kids, stringsAsFactors = FALSE)
# Merging two created data frame
merge <- merge(d1, d2, by.x = "d1.Kids", by.y = "d2.Kids", all = TRUE)
print(merge)
Output should be:
kids ages states
1 Jack 12 CA
2 Jill 10 MA
3 Jillian 7 DE
4 John NA HI
5 James NA PA
6 Mary 30 NA