0

I have two dataframes, each of them have a column that contains several words. However the order and the size of this two column is different. The first database is bigger and looks like this:

words response  time.response
WORD1   s           434
WORD2   s           567
WORD3   l           765 
WORD2   s           235
WORD4   l           854
WORD3   l           521 
...

as you can see some words occurs several times the second dataframe is smaller and looks like this:

words  Assoc.  Dist
WORD1    4       1
WORD2    7       0
WORD3    6       1
WORD4    2       1
...

How can I associate the value of the second dataframe to the words of the first one, knowing that they are the same?

I expect something like this:

words response  time.response  Assoc   Dist
WORD1   s           434          4      1
WORD2   s           567          7      0
WORD3   l           765          6      1
WORD2   s           235          7      0
WORD4   l           854          2      1
WORD3   l           521          6      1

...

Khashaa
  • 7,293
  • 2
  • 21
  • 37
Vittorio
  • 1
  • 1

1 Answers1

1

Probably the most straightforward method is merge:

merge(df, myTable, by="words", all.x=T)

A second, maybe less practical method is to use match together with cbind:

cbind(df, myTable[match(df$words, myTable$words), -1])

This method works like a lookup table: match function finds the corresponding rows in in your main dataset where the words match. We use these row indices to reorganize the shorter data.frame, repeating rows where necessary and dropping the "words" column with "-1." Finally, we use cbind to combine the matched rows of the shorter dataset onto the larger one.

data

df <- read.table(header=T, text="words response  time.response
WORD1   s           434
WORD2   s           567
WORD3   l           765 
WORD2   s           235
WORD4   l           854
WORD3   l           521")

myTable <- read.table(header=T, text="words  Assoc.  Dist
WORD1    4       1
WORD2    7       0
WORD3    6       1
WORD4    2       1")
lmo
  • 37,904
  • 9
  • 56
  • 69
  • Thank you very much. If I well understood the merge function works only if the two tables or df have a column with the same name. Instead, the second method is more flexible, what metter is that the same value are contained in both the tables. – Vittorio Jun 05 '16 at 21:27