I want to make a data frame that has a single variable with 345 entries as follows: entries 1:149 to be "2020", entries 150:297 to be "2021" and entries 298:345 to be "2022".
I am entirely not sure how to put this together. I feel like it is a simple problem but I have been at this for an hour with no progress. I would extremely appreciate any help!
CodePudding user response:
You can use rep
to repeat a string for a desired number of time.
data.frame(year = c(rep("2020", 149),
rep("2021", 297 - 150 1),
rep("2022", 345 - 298 1)))
Or set a dataframe with an empty column named year
, and set row values accordingly.
df <- data.frame(year = "")
df[1:149,] <- "2020"
df[150:297, ] <- "2021"
df[298:345,] <- "2022"
CodePudding user response:
You can use the times
argument in rep
to make this a bit easier:
df <- data.frame(year = as.character(rep(2020:2022, times = c(149, 148, 48))))