Returns a vector with the length equal to the number of rows in the
data
with the baseline value of the var
repeated for every
time
value of each id
.
repeat_baseline_values(data, var, id, time, baseline_level, repeat_NA = TRUE)
data | A data frame. |
---|---|
var | The bare (unquoted) name of the column with the values to be repeated. |
id | The bare (unquoted) name of the column that identifies each subject. |
time | The bare (unquoted) name of the column with the time values. |
baseline_level | The value of |
repeat_NA | A logical vector indicating whether or not |
A vector of the same lenght and class of var
.
df <- data.frame( id = rep(1:5, each = 4), time = rep(1:4, 5), score = rnorm(20, mean = 10, sd = 2) ) df$baseline_score <- repeat_baseline_values(df, score, id, time, 1) df#> id time score baseline_score #> 1 1 1 8.415576 8.415576 #> 2 1 2 12.913287 8.415576 #> 3 1 3 6.380076 8.415576 #> 4 1 4 9.284441 8.415576 #> 5 2 1 8.800278 8.800278 #> 6 2 2 5.671336 8.800278 #> 7 2 3 8.553389 8.800278 #> 8 2 4 9.316911 8.800278 #> 9 3 1 9.129507 9.129507 #> 10 3 2 14.677477 9.129507 #> 11 3 3 10.435740 9.129507 #> 12 3 4 12.258709 9.129507 #> 13 4 1 9.456454 9.456454 #> 14 4 2 11.021083 9.456454 #> 15 4 3 9.264856 9.456454 #> 16 4 4 6.428344 9.456454 #> 17 5 1 7.767082 7.767082 #> 18 5 2 8.574506 7.767082 #> 19 5 3 12.816592 7.767082 #> 20 5 4 10.767092 7.767082