I am building a R data frame (or data table) starting with a sequence of times--there are multiple values for each hour. I'll call it sim
sim <- data.frame(hr = rep(1:2,each=3)
I wish to assign parameters from another data frame ('pars'), which has one value for each hour, to the first data frame, by matching the hour
pars <- data.frame(f= c(0.2,0.3), hr = 1:2)
The sim data frame should look like this:
sim
hr f
1 0.3
1 0.3
1 0.3
2 0.2
2 0.2
2 0.2
I have tried
sim$f <- pars$f[(which(pars$hr==sim$hr)]
and
sim$f <- pars$f[sim['hr']==pars$hr]
but these produce a lot of 'NAs'