Reputation: 485
I have 2 data frames :
p1=rnorm(10)
p2=rnorm(10)
p3=rnorm(10)
pf=data.frame(p1,p2,p3)
pf
p1 p2 p3
1 -1.4899362 0.44024186 0.5949573
2 1.1727063 0.15883062 -1.4196451
3 -1.4798270 0.65976414 -1.6066772
4 -0.4303878 2.22051966 0.8929259
5 -1.0516386 -1.18394507 0.1481680
6 1.5225863 -0.07395583 1.2270284
7 0.5928281 -0.41635467 -0.7618043
8 -0.2226615 -0.19148234 0.4193754
9 0.7128943 0.06954478 -1.0399434
10 0.7166008 1.15534832 0.7115740
and
f1<-c(1,2,3,4,5,1,2,3,4,5)
f2<-c(1,2,1,2,3,4,5,6,7,8)
f3<-c(1,2,3,4,5,6,7,1,2,3)
ff<-data.frame(f1,f2,f3)
> ff
f1 f2 f3
1 1 1 1
2 2 2 2
3 3 1 3
4 4 2 4
5 5 3 5
6 1 4 6
7 2 5 7
8 3 6 1
9 4 7 2
10 5 8 3
How can I split the data frame so that I can have a data frame like this:
I would like to split data pf according ff which contains series
p1 p1x p2 p2x p3 p3x
1 -1.4899362 1.5225863 0.44024186 0.65976414 0.5949573 0.4193754
2 1.1727063 0.5928281 0.15883062 2.22051966 -1.4196451 -1.0399434
3 -1.4798270 -0.2226615 NA -1.18394507 -1.6066772 0.7115740
4 -0.4303878 0.7128943 NA -0.07395583 0.8929259 NA
5 -1.0516386 0.7166008 NA -0.41635467 0.1481680 NA
6 NA NA NA -0.19148234 1.2270284 NA
7 NA NA NA 0.06954478 -0.7618043 NA
8 NA NA NA 1.15534832 NA NA
Thanks so much for your help.
Upvotes: 1
Views: 215
Reputation: 4474
You haven't provided a seed, but here a possible base R solution:
#find out where to split
split_df=as.data.frame(rbind(matrix(0,ncol=ncol(pf)),
apply(diff(as.matrix(ff))!=1,2,cumsum)))
#perform split for each column
split_data=unlist(Map(function(x,y) split(x,y),
pf,
split_df),recursive=FALSE)
#now bind the data together
max_length=max(sapply(split_data,length))
sapply(split_data,function(x) `[<-`(rep(NA_real_,max_length),
1:length(x),
x))
Upvotes: 3