-1

I have a dataframe like this:

name
Google
Amazon
Google
Yahoo
Google
Yahoo

And another one like this:

name   stock_name
Google  stockA
Amazon  stockB
Yahoo   stockC

The final result I would like to take from the merge of these 2 dataframes:

name stock_name
Google stockA
Amazon stockB
Google stockA
Yahoo  stockC
Google stockA
Yahoo  stockC

Using the merge it is not possible to make it. Any hind on how could make it?

Keri
  • 375
  • 1
  • 3
  • 14

2 Answers2

1

if you call your data frames df1 and df2 you could do

 df1$stock_name <- df2$stock_name[match(df1$name, df2$name)]
Mouad_Seridi
  • 2,666
  • 15
  • 27
0

Another way to solve this:

df1 <- data.frame(data = c("Google", "Amazon", "Google", "Yahoo", "Google", "Yahoo"))

df2 <- data.frame(data = c("Google" = "stockA", "Amazon"  = "stockB", "Yahoo" = "stockC"))
colnames(df2) <- "stock_name"

df1$stock_name <- df2[df1$data, 1]
Fábio
  • 771
  • 2
  • 14
  • 25