Reputation: 93
I am trying to assign values from market data set (length of 997 objects) to another (t_final) data set (length of 680 obj) based on certain criteria.
market_dt consists of columns: ref.date and ret.closing.prices t_final consists of columns: date and compound
both datasets have dates in same format YYYY-MM-DD
I wanted to make a loop working as such (see below) that my ret.closing.prices value will be copied to a new column in t_final dataset IF the dates match.
for i in 1:997 {
if (market_dt[]$ref.date == t_final[]$date){
t_final[]$return <- market_dt[]$ret.closing.prices
}
However I get the errors:
Warning messages:
1: In `==.default`(market_dt[]$ref.date, t_final[]$date) :
longer object length is not a multiple of shorter object length
2: In if (market_dt[]$ref.date == t_final[]$date) { :
the condition has length > 1 and only the first element will be used
How can I properly make this work?
Thank you!
Upvotes: 0
Views: 531
Reputation: 594
You can use 'join' function:
library(dplyr)
left_join(t_final, market_dt, by = c("date" = "ref.date")) %>%
rename(return = ret.closing.prices)
Upvotes: 1