In the below, the data frame index denotes the value while t1:t2 denotes the number of times that specific value was recorded at a specific point in time. For example index 10 at t1 equals 1 suggesting that it was made 1 records; at t2 there are 4 records, whole at t3 and t4 just 1. I would like to return the values from columns t1:t4 based on an index column
Input:
index t1 t2 t3 t4
10 1 4 1 1
20 2 5 1 0
30 3 6 1 0
40 0 0 0 2
Output:
t1 t2 t3 t4
10 10 10 10
20 10 20 40
20 10 30 40
30 10 NA NA
30 20 NA NA
30 20 NA NA
NA 20 NA NA
NA 20 NA NA
NA 30 NA NA
NA 30 NA NA
NA 30 NA NA
NA 30 NA NA
NA 30 NA NA
NA 30 NA NA
Sample data:
df<-structure(list(index=c (10,20,30,40),
t1 = c(1, 2, 3, 0),
t2 = c(4, 5, 6, 0),
t3 = c(1, 1,1, 0),
t4 = c(1, 0, 0, 2)), row.names = c(NA,4L), class = "data.frame")
df