0

this question is related to the post below, however, this is the more general from.

How to split the data by time more efficiently?

I have data, which repeatedly observed ID 1. The value is changing through time. interval is time interval when observation start and observation stop.

ID<-rep(1,4)
start<-c(0, 1, 4, 5)
stop<-c(1, 4, 5, 7)
sex<-c("M","M","M","M")
value<-c(10.5,20,13,19)
test<-data.frame(ID,start,stop,sex,value)
test<-test%>%mutate(rep=stop-start)

  ID start stop sex value interval
1  1     0    1   M  10.5        1
2  1     1    4   M  20.0        3
3  1     4    5   M  13.0        1
4  1     5    7   M  19.0        2

I want to duplicate the row and stack it repeatedly. In duplicating, I want (interval-1) copies of row. So,

  ID start stop sex value interval
1  1     0    1   M  10.5        1
2  1     1    4   M  20.0        3
2  1     1    4   M  20.0        3
2  1     1    4   M  20.0        3
3  1     4    5   M  13.0        1
4  1     5    7   M  19.0        2
4  1     5    7   M  19.0        2

second column of test duplicated two times(which is 3-1) and stacked.

I tried for loop, but it was very complicated. Is there a way to do it with for loop?

ESKim
  • 422
  • 4
  • 14

1 Answers1

-1

We can use tidyr::uncount

tidyr::uncount(test, rep, .remove = FALSE)

#    ID start stop sex value rep
#1    1     0    1   M  10.5   1
#2    1     1    4   M  20.0   3
#2.1  1     1    4   M  20.0   3
#2.2  1     1    4   M  20.0   3
#3    1     4    5   M  13.0   1
#4    1     5    7   M  19.0   2
#4.1  1     5    7   M  19.0   2

Or in base R, we can use rep

test[rep(1:nrow(test), test$rep), ]
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213