I have two data frames in R:
df1 <- data.frame("name" = c("jack", "william", "david", "john", "peter", "aaron"),
"city" = c("LA","HK","NY","TK", "PARIS","SYDNEY"))
df2 <- data.frame("name" = c("jack", "william", "david", "john", "peter", "aaron", "jack", "william",
"david", "john"),
"fruit" = c("apple", "pear", "kiwi", "peach", "avocado", "orange", "pineapple", "pear",
"watermelon", "rockmelon"))
I want to pick up all the "fruit" information for everyone (from df2), then add to df1, so I use this:
match <- match(df1$name, df2$name)
df1$fruit=df2$fruit(match)
I get "Error: attempt to apply non-function". how do I overcome the fact that one person may have multiple fruits in df2?
Many thanks!