0

A seemgly easy question, for which I can't find a solution. I even tried work arounds with for and if loops.

I have a long data frame and a short data frame. I want to add information from the short data frame to the long data frame at the correct positions. In other words: I want to append a column to an existing data frame, based on a conditional statement from a shorter data frame. So I want to merge data frames based on a conditional statement with characters.

data.frame.short <- data.frame(names=c("John", "Mary", "Achmed", "Ali", "Lin"), age=c(23, 34, 44, 42, 52))
data.frame.long <-  data.frame(names=c("Mary", "Jamilla", "Achmed", "John", "Ali", "Lin", "Michael", "Abdul"))

The result should be the added column age of data.frame.short to the data.frame.long, at the correct positions. Everything else should be filled with NA. Thus, the results should be:

data.frame.long.appended <-  data.frame(names=c("Mary", "Jamilla", "Achmed", "John", "Ali", "Lin", "Michael", "Abdul"), age=c(34, NA, 44, 23, 42, 52, NA, NA))
Tahwan
  • 39
  • 6

0 Answers0