Skip to content
Prev 327379 / 398502 Next

How to split two levels several times?

Hello,

As for the first question, note that in the case you describe, the 
resulting list of df's will not be a split of the original, there will 
be a duplication in the final 4-1 and 1-3. The following is a hack but 
will do it.


lens <- rle(as.character(XXX$electrode))$lengths
m <- length(lens) %/% 2
idx <- rep(1:m, sapply(1:m, function(.m) sum(lens[(2*.m - 1):(2*.m)])))
if(length(lens) %% 2 != 0)
	idx <- c(idx, rep(m + 1, lens[length(lens)]))

sp <- split(XXX, idx)

if(length(lens) %% 2 != 0){
	idx2 <- sp[[m]]$electrode == sp[[m]]$electrode[nrow(sp[[m]])]
	sp[[m + 1]] <- rbind(sp[[m]][idx2, ], sp[[m + 1]])
}
sp


As for the second question, I'm not understanding it, can you post 
sample output?

Rui Barradas

Em 24-07-2013 13:58, dennis1991 at gmx.net escreveu: