pad_int fills the gaps in incomplete integer variables. It will insert a record for each of the missing value. For all other variables in the data frame a missing value will be inserted at the padded rows.

pad_int(x, by, start_val = NULL, end_val = NULL, group = NULL, step = 1)

Arguments

x

A data frame.

by

The column to be padded.

start_val

The first value of the returned variable. If NULL it will use the lowest value of the input variable.

end_val

The last value of the returned variable. If NULL it will use the highest value of the input variable.

group

Optional character vector that specifies the grouping variable(s). Padding will take place within the different group values.

step

The step size of the returned variable.

Value

The data frame x with the specified variable padded. All non-grouping variables in the data frame will have missing values at the rows that are padded.

Examples

int_df <- data.frame(x = c(2005, 2007, 2008, 2011), val = c(3, 2, 6, 3)) pad_int(int_df, 'x')
#> x val #> 1 2005 3 #> 2 2006 NA #> 3 2007 2 #> 4 2008 6 #> 5 2009 NA #> 6 2010 NA #> 7 2011 3
pad_int(int_df, 'x', start_val = 2006, end_val = 2013)
#> x val #> 1 2006 NA #> 2 2007 2 #> 3 2008 6 #> 4 2009 NA #> 5 2010 NA #> 6 2011 3 #> 7 2012 NA #> 8 2013 NA
int_df2 <- data.frame(x = c(2005, 2015), val = c(3, 4)) pad_int(int_df2, 'x', step = 2)
#> x val #> 1 2005 3 #> 2 2007 NA #> 3 2009 NA #> 4 2011 NA #> 5 2013 NA #> 6 2015 4
pad_int(int_df2, 'x', step = 5)
#> x val #> 1 2005 3 #> 2 2010 NA #> 3 2015 4
int_df3 <- data.frame(x = c(2005, 2006, 2008, 2006, 2007, 2009), g = rep(LETTERS[1:2], each = 3), val = c(6, 6, 3, 5, 4, 3)) pad_int(int_df3, 'x', group = 'g')
#> x g val #> 1 2005 A 6 #> 2 2006 A 6 #> 3 2007 A NA #> 4 2008 A 3 #> 5 2006 B 5 #> 6 2007 B 4 #> 7 2008 B NA #> 8 2009 B 3
pad_int(int_df3, 'x', group = 'g', start_val = 2005, end_val = 2009)
#> x g val #> 1 2005 A 6 #> 2 2006 A 6 #> 3 2007 A NA #> 4 2008 A 3 #> 5 2009 A NA #> 6 2005 B NA #> 7 2006 B 5 #> 8 2007 B 4 #> 9 2008 B NA #> 10 2009 B 3