-1

I have looked around a lot but cant figure this one out. I have one data frame that has multiple rows for the same date ( 2014-05-13 date has 15 rows) then I have a data frame 2 with only one value for each date. How can I merge the two and repeat the value from data frame 2 for all repeating date rows?

enter image description here

enter image description here

Using:

master = merge(returns,count_msg, by = "date", all=TRUE) 

merges the second data frame but introduces new rows that are NA for word count. My end goal is to do regression analysis on word count vs. daily returns.

alistaire
  • 42,459
  • 4
  • 77
  • 117
user2420956
  • 67
  • 1
  • 9
  • try using na.locf("column name") for replacing NA values – Ishan Juneja May 06 '17 at 16:35
  • 1
    Please do not paste links to images of data. Take time to enter the data into the site or people will typically not look at it. – manotheshark May 06 '17 at 16:38
  • 1
    Please read about [how to make your example reproducible](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example#5963610). – alistaire May 06 '17 at 16:40

1 Answers1

1

You could just join the dataframes using dplyr.

library(dplyr)
master <- count %>% left_join(returns, by = "date")

I'm guessing here you don't want the returns for dates when you don't have any word counts.

Sraffa
  • 1,658
  • 12
  • 27
  • Thanks for the response, so I loaded the returns data on only the date range found in the count_msg table, so I just need to repeat the returns for every message the user posted, i.e. user posted ten messages on the same date so repeat the return ten times for that day; when I put in your code it gave me the error: cannot join a Date object with an object that is not a Date object. I have never used date objects, right now the class is simple data frame with numeric columns. – user2420956 May 06 '17 at 18:09
  • Yeah, both "date" columns need to be the same type. Try setting `returns$date <- as.Date(returns$date)` and `count$date <- as.Date(count$date)` before the join. I'm guessing one of both date columns already is a date and the other is a character. – Sraffa May 06 '17 at 18:25
  • @user2420956 This type of problems is the reason why you shouldn't post images of your data. Please, post `dput(...)` as explained [here](http://stackoverflow.com/a/5963610/3817004). – Uwe May 06 '17 at 22:29