2

It might be simple, but I am not finding the solution I have a tibble of values for one year and would generate the same set for the next 40 years. A small example would look like this (the real data is 2000 x 6)

data <- as_tibble(data.frame(yr =c(2010,2010,2010), values = c("a","b","c")))

Now, I would like have the same set of observations of "values" for the years 2011 until 2050. Any idea how to do this in R?

Thanks Renger

arnyeinstein
  • 669
  • 1
  • 5
  • 14

3 Answers3

5

Check this solution:

tidyr::crossing(
  yr = 2010:2050,
  values = c("a","b","c")
)
Paweł Chabros
  • 2,349
  • 1
  • 9
  • 12
1

Base R way using lapply

lapply(2011:2050, function(x) data.frame(yr = x, values = data$values))

This would give you a list of dataframes for each year.

Output for 3 values

lapply(2011:2013, function(x) data.frame(yr = x, values = data$values))


#[[1]]
#    yr values
#1 2011      a
#2 2011      b
#3 2011      c

#[[2]]
#    yr values
#1 2012      a
#2 2012      b
#3 2012      c

#[[3]]
#    yr values
#1 2013      a
#2 2013      b
#3 2013      c
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
0

Here's an alternative too. Useful if you're modeling:

library(modelr)
library(dplyr)
data1 %>% 
  data_grid(yr=2010:2050,values=values)
NelsonGon
  • 13,015
  • 7
  • 27
  • 57