0

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'

  • Please check your data.frame code. There are some typos i.e. `==` instead of `=` and `rep` - `rep(1,2,each=3)` perhaps, you meant `rep(1:2, each = 3)` – akrun Dec 23 '21 at 19:36
  • It is a join/merge i.e. `merge(sim, pars, all.x = TRUE)` – akrun Dec 23 '21 at 19:37
  • @akrun The 'pars' dataset in real life is >100 columns wide, I cannot merge it to the 'sim' simulated data set I am building – Seth W. Bigelow Dec 23 '21 at 19:56
  • Not clear though. I am assuming that if you want only the 'f' column, then subset the 'pars' with the 'hr', 'f' columns and merge i..e `merge(sim, pars[c('hr', 'f')], all.x = TRUE)` – akrun Dec 23 '21 at 19:57

0 Answers0