Home > Mobile >  How to fill a range of cells within a column with particular values?
How to fill a range of cells within a column with particular values?

Time:12-21

I have a df in R with 15,767 observations of 24 variables. Below is a reproducible example:

data <- data %>% add_column(year = NA)

I would now like to fill that column with certain values. In particular, I would like to add "2011" to rows 1 through 18, "2012" to rows 19 through 200, and so on. What is the easiest way to do this? I have had trouble finding a simple solution online.

CodePudding user response:

This is hardly reproducible, but if you have

newvals <- data.frame(
  year = c(2011L, 2012L),
  lengths = c(18, 182)
)
rep(newvals$year, times = newvals$lengths)
#   [1] 2011 2011 2011 2011 2011 2011 2011 2011 2011 2011 2011 2011 2011 2011 2011 2011 2011 2011
#  [19] 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012
#  [37] 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012
#  [55] 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012
#  [73] 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012
#  [91] 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012
# [109] 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012
# [127] 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012
# [145] 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012
# [163] 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012
# [181] 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012 2012
# [199] 2012 2012

(There's no strict reason to start with a data.frame, it works just fine with literals, e.g., rep(2011:2012, times = c(18, 182)), but sometimes a frame is a convenient way to visualize/edit/maintain things like that. Over to you.)

I suspect that a better way should exist, though, as a condition of the actual data.

CodePudding user response:

Maybe something like this?

library(dplyr)

# define your vector
my_vec = c(rep(2011, 18), rep(2012, 200-19))

# fill example dataframe with 1000 rows with your vector:

tibble(col1 = 1:1000) %>% 
  add_column(year = rep_len(my_vec, length.out = nrow(.)))
  • Related