I have two vectors, which I would like to combine in one dataframe. One of the vectors values
needs to be divided into two columns. The second vector nc
informs about the number of values for each observation. If nc
is 1, only one value is given in values
(which goes into val1
) and 999
is to be written in the second column (val2
).
What is an r-ish way to divide vector value
and populate the two columns of df
? I suspect I miss something very obvious, but can't proceed at the moment...Many thanks!
set.seed(123)
nc <- sample(1:2, 10, replace = TRUE)
value <- sample(1:6, sum(nc), replace = TRUE)
# result by hand
df <- data.frame(nc = nc,
val1 = c(6, 3, 4, 1, 2, 2, 6, 5, 6, 5),
val2 = c(999, 5, 999, 6, 1, 999, 6, 4, 4, 999))