I have a data frame which is configured roughly like this:
df <- cbind(c('hello', 'yes', 'example'),c(7,8,5),c(0,0,0))
| words | frequency | count |
|---|---|---|
| hello | 7 | 0 |
| yes | 8 | 0 |
| example | 5 | 0 |
What I'm trying to do is add values to the third column from a different data frame, which is similiar but looks like this:
df2 <- cbind(c('example','hello') ,c(5,6))
| words | frequency |
|---|---|
| example | 5 |
| hello | 6 |
My goal is to find matching values for the first column in both data frames (they have the same column name) and add matching values from the second data frame to the third column of the first data frame.
The result should look like this:
df <- cbind(c('hello', 'yes', 'example'),c(7,8,5),c(6,0,5))
| words | frequency | count |
|---|---|---|
| hello | 7 | 6 |
| yes | 8 | 0 |
| example | 5 | 5 |
What I've tried so far is:
df <- merge(df,df2, by = "words", all.x=TRUE)
However, it doesn't work.
I could use some help understanding how could it be done. Any help will be welcome.
freqtocountand then you can left join by wordscbinddoesn't create data frames, it creates matrices. Usedata.frameinstead ofcbind, and put the column names in there too, for the example to make sense.