[R] How to split two levels several times?
dennis1991 at gmx.net
dennis1991 at gmx.net
Mon Jul 29 15:20:25 CEST 2013
Hi Arun, thanks. Great help. I tested the code for several tables and your function works well.
Dennis
> Gesendet: Freitag, 26. Juli 2013 um 15:43 Uhr
> Von: arun <smartpink111 at yahoo.com>
> An: "dennis1991 at gmx.net" <dennis1991 at gmx.net>
> Cc: "R help" <r-help at r-project.org>, "Rui Barradas" <ruipbarradas at sapo.pt>
> Betreff: Re: [R] How to split two levels several times?
>
> It would be better to wrap it in a function.
>
> fun1<- function(x,colName,N,value){
> rl<- rle(as.character(x[,colName]))
> dat<-do.call(rbind,lapply(seq_along(rl$lengths),function(i){x1<-if(rl$values[i]==value & (rl$lengths[i]%/%N>1)) rep(N,rl$lengths[i]%/%N) else rl$lengths[i];data.frame(Len=x1,Val=rl$values[i])}))
> lst1<-split(cumsum(dat[,1]),((seq_along(dat[,1])-1)%/%2)+1)
> vec1<-sapply(lst1,max)
> vec2<-c(1,vec1[-length(vec1)]+1)
> res<- lapply(seq_along(lst1),function(i) {x1<-lst1[[i]]; x[seq(vec2[i],max(x1)),]})
> res
> }
>
> fun1(XXX,"electrode",6,"electrode4")
> #Using previous dataset XXX, XXX1, XXX2
> fun1(XXX,"electrode",3,"electrode1")
>
> fun1(XXX1,"electrode",3,"electrode1")
>
> fun1(XXX2,"electrode",3,"electrode1")
> A.K.
>
> ----- Original Message -----
> From: arun <smartpink111 at yahoo.com>
> To: "dennis1991 at gmx.net" <dennis1991 at gmx.net>
> Cc: R help <r-help at r-project.org>; Rui Barradas <ruipbarradas at sapo.pt>
> Sent: Friday, July 26, 2013 9:26 AM
> Subject: Re: [R] How to split two levels several times?
>
>
>
> Hi Dennis,
> I guess in this case, instead of "Eletrode1" occuring 3 times, it is "Electrode4" exists only 6 times. If that is the situation:
> just change:
> XXX: data
> rl<-rle(as.character(XXX$electrode))
> dat<-do.call(rbind,lapply(seq_along(rl$lengths),function(i){x1<-if(rl$values[i]=="electrode4" & (rl$lengths[i]%/%6>1)) rep(6,rl$lengths[i]%/%6) else rl$lengths[i];data.frame(Len=x1,Val=rl$values[i])}))
> lst1<-split(cumsum(dat[,1]),((seq_along(dat[,1])-1)%/%2)+1)
> vec1<-sapply(lst1,max)
> vec2<-c(1,vec1[-length(vec1)]+1)
> res<- lapply(seq_along(lst1),function(i) {x1<-lst1[[i]]; XXX[seq(vec2[i],max(x1)),]})
> res
> [[1]]
> electrode length
> 1 electrode1 206
> 2 electrode1 194
> 3 electrode1 182
> 4 electrode1 172
> 5 electrode1 169
> 6 electrode2 82
> 7 electrode2 78
> 8 electrode2 70
> 9 electrode2 58
>
> [[2]]
> electrode length
> 10 electrode1 206
> 11 electrode1 194
> 12 electrode1 182
> 13 electrode1 172
> 14 electrode1 169
> 15 electrode3 260
> 16 electrode3 176
> 17 electrode3 137
>
> [[3]]
> electrode length
> 18 electrode1 206
> 19 electrode1 194
> 20 electrode1 182
> 21 electrode1 172
> 22 electrode1 169
> 23 electrode4 86
> 24 electrode4 66
> 25 electrode4 64
> 26 electrode4 52
> 27 electrode4 27
> 28 electrode4 26
>
> [[4]]
> electrode length
> 29 electrode2 82
> 30 electrode2 78
> 31 electrode2 70
> 32 electrode2 58
> 33 electrode1 206
> 34 electrode1 194
> 35 electrode1 182
> 36 electrode1 172
> 37 electrode1 169
>
> [[5]]
> electrode length
> 38 electrode2 82
> 39 electrode2 78
> 40 electrode2 70
> 41 electrode2 58
> 42 electrode3 260
> 43 electrode3 176
> 44 electrode3 137
>
> [[6]]
> electrode length
> 45 electrode2 82
> 46 electrode2 78
> 47 electrode2 70
> 48 electrode2 58
> 49 electrode4 86
> 50 electrode4 66
> 51 electrode4 64
> 52 electrode4 52
> 53 electrode4 27
> 54 electrode4 26
>
> [[7]]
> electrode length
> 55 electrode3 260
> 56 electrode3 176
> 57 electrode3 137
> 58 electrode1 206
> 59 electrode1 194
> 60 electrode1 182
> 61 electrode1 172
> 62 electrode1 169
>
> [[8]]
> electrode length
> 63 electrode3 260
> 64 electrode3 176
> 65 electrode3 137
> 66 electrode2 82
> 67 electrode2 78
> 68 electrode2 70
> 69 electrode2 58
>
> [[9]]
> electrode length
> 70 electrode3 260
> 71 electrode3 176
> 72 electrode3 137
> 73 electrode4 86
> 74 electrode4 66
> 75 electrode4 64
> 76 electrode4 52
> 77 electrode4 27
> 78 electrode4 26
>
> [[10]]
> electrode length
> 79 electrode4 86
> 80 electrode4 66
> 81 electrode4 64
> 82 electrode4 52
> 83 electrode4 27
> 84 electrode4 26
> 85 electrode1 206
> 86 electrode1 194
> 87 electrode1 182
> 88 electrode1 172
> 89 electrode1 169
>
> [[11]]
> electrode length
> 90 electrode4 86
> 91 electrode4 66
> 92 electrode4 64
> 93 electrode4 52
> 94 electrode4 27
> 95 electrode4 26
> 96 electrode2 82
> 97 electrode2 78
> 98 electrode2 70
> 99 electrode2 58
>
> [[12]]
> electrode length
> 100 electrode4 86
> 101 electrode4 66
> 102 electrode4 64
> 103 electrode4 52
> 104 electrode4 27
> 105 electrode4 26
> 106 electrode3 260
> 107 electrode3 176
> 108 electrode3 137
>
>
> A.K.
>
>
>
>
> ----- Original Message -----
> From: "dennis1991 at gmx.net" <dennis1991 at gmx.net>
> To: Rui Barradas <ruipbarradas at sapo.pt>; r-help at r-project.org
> Cc:
> Sent: Friday, July 26, 2013 6:07 AM
> Subject: Re: [R] How to split two levels several times?
>
> Hi Rui & Arun,
> really thanks for investing so much time to deal with this problem! The code works now for this specific example. However it is not generally robust for slightly different situations. For instance it cannot correctly handle a slight variation of the table where I have again 4 types of electrodes of certain lengths. Electrode4 exists only 6 times. At the transition of the combinations 3-4 and 4-1 there are 12 times electrode4 which stick together in the output $`9`. This leads to wrong splittings thereafter. Sorry for asking such tricky questions.
>
> New table XXX
>
> electrode length
> electrode1 206
> electrode1 194
> electrode1 182
> electrode1 172
> electrode1 169
> electrode2 82
> electrode2 78
> electrode2 70
> electrode2 58
> electrode1 206
> electrode1 194
> electrode1 182
> electrode1 172
> electrode1 169
> electrode3 260
> electrode3 176
> electrode3 137
> electrode1 206
> electrode1 194
> electrode1 182
> electrode1 172
> electrode1 169
> electrode4 86
> electrode4 66
> electrode4 64
> electrode4 52
> electrode4 27
> electrode4 26
> electrode2 82
> electrode2 78
> electrode2 70
> electrode2 58
> electrode1 206
> electrode1 194
> electrode1 182
> electrode1 172
> electrode1 169
> electrode2 82
> electrode2 78
> electrode2 70
> electrode2 58
> electrode3 260
> electrode3 176
> electrode3 137
> electrode2 82
> electrode2 78
> electrode2 70
> electrode2 58
> electrode4 86
> electrode4 66
> electrode4 64
> electrode4 52
> electrode4 27
> electrode4 26
> electrode3 260
> electrode3 176
> electrode3 137
> electrode1 206
> electrode1 194
> electrode1 182
> electrode1 172
> electrode1 169
> electrode3 260
> electrode3 176
> electrode3 137
> electrode2 82
> electrode2 78
> electrode2 70
> electrode2 58
> electrode3 260
> electrode3 176
> electrode3 137
> electrode4 86
> electrode4 66
> electrode4 64
> electrode4 52
> electrode4 27
> electrode4 26
> electrode4 86
> electrode4 66
> electrode4 64
> electrode4 52
> electrode4 27
> electrode4 26
> electrode1 206
> electrode1 194
> electrode1 182
> electrode1 172
> electrode1 169
> electrode4 86
> electrode4 66
> electrode4 64
> electrode4 52
> electrode4 27
> electrode4 26
> electrode2 82
> electrode2 78
> electrode2 70
> electrode2 58
> electrode4 86
> electrode4 66
> electrode4 64
> electrode4 52
> electrode4 27
> electrode4 26
> electrode3 260
> electrode3 176
> electrode3 137
>
>
>
>
>
> > Gesendet: Donnerstag, 25. Juli 2013 um 20:53 Uhr
> > Von: "Rui Barradas" <ruipbarradas at sapo.pt>
> > An: dennis1991 at gmx.net
> > Cc: r-help at r-project.org
> > Betreff: Re: Aw: Re: Re: Re: [R] How to split two levels several times?
> >
> > Hello,
> >
> > I think the following does what you want. (I don't know if it makes much
> > sense but it works.)
> >
> >
> >
> > 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_idx <- split(idx, idx)
> > n <- length(sp_idx[[m]])
> > if(n %/% 2 < length(sp_idx[[m + 1]]))
> > sp_idx[[m]][(n %/% 2 + 1):n] <- sp_idx[[m + 1]][1]
> > else
> > sp_idx[[m]][(n - length(sp_idx[[m + 1]]) + 1):n] <- sp_idx[[m + 1]][1]
> > idx <- unlist(sp_idx)
> > }
> >
> > sp <- split(XXX, idx)
> > sp
> >
> >
> >
> > Rui Barradas
> >
> > Em 25-07-2013 11:40, dennis1991 at gmx.net escreveu:
> > > Hi Rui
> > > once more thank you for your help. But the code does so far not solve the problem because it still treats rows 17-22 (repeated appearance of electrode1) as one single level. However as can be seen by rows 1-3 (or rows 17-19 and rows 20-22) and the order of the length variable (row 1 = 5.7, row 2 = 6.3, row 3 = 6.2) electrode1 consists only of 3 rows. Maybe that was not made absolutely clear by me. As described in my mail before if by chance (or systematically) it happens to be that electrode1 appears right after each other in the table then the code should split it “half way”.
> > >
> > > So idx should not return
> > > [1] 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4
> > >
> > > but instead 6 times number 4 at the end
> > > [1] 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4
> > >
> > > Do you have any solution?
> > >
> > >
> > >> Gesendet: Mittwoch, 24. Juli 2013 um 23:47 Uhr
> > >> Von: "Rui Barradas" <ruipbarradas at sapo.pt>
> > >> An: dennis1991 at gmx.net
> > >> Cc: r-help at r-project.org
> > >> Betreff: Re: Aw: Re: Re: [R] 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:
> > >>> Hi Rui
> > >>> the splitting code worked fine. Thanks for your help. Now I realized that the code cannot handle a table with levels that by chance (or systematically) repeatedly appear after each other. For instance this may happen if I need to extract the final two pairs of the table XXX below: electrode4+electrode1 and electrode1+electrode3.
> > >>>
> > >>> lens <- rle(as.character(XXX$electrode))$lengths
> > >>> will return 3 2 3 2 6 6 3 and not 3 2 3 2 6 3 3 3 because it counts electrode1 double.
> > >>> split(XXX, idx) will produce 3 incorrect outputs instead of the required 4.
> > >>> This will also occur if I have systematic combinations 1-4 after each other for instance in a new table “XX” below where electrode4 appears twice.
> > >>>
> > >>> Is there a way to make splitting "half-way" between two of the same levels possible by predefining the length of each individual level? This would make the splitting code more robust. Thanks for advice.
> > >>>
> > >>>
> > >>> This is the table "XXX"
> > >>>
> > >>> electrode length
> > >>>
> > >>> electrode1 5.7
> > >>> electrode1 6.3
> > >>> electrode1 6.2
> > >>> electrode2 11.4
> > >>> electrode2 9.7
> > >>> electrode3 14.2
> > >>> electrode3 14.8
> > >>> electrode3 12.6
> > >>> electrode2 11.4
> > >>> electrode2 9.7
> > >>> electrode4 17.0
> > >>> electrode4 16.3
> > >>> electrode4 17.8
> > >>> electrode4 18.3
> > >>> electrode4 16.9
> > >>> electrode4 18.5
> > >>> electrode1 5.7
> > >>> electrode1 6.3
> > >>> electrode1 6.2
> > >>> electrode1 5.7
> > >>> electrode1 6.3
> > >>> electrode1 6.2
> > >>> electrode3 14.2
> > >>> electrode3 14.8
> > >>> electrode3 12.6
> > >>>
> > >>>
> > >>> This is a simplified table XX
> > >>>
> > >>> electrode1
> > >>> electrode2
> > >>> electrode1
> > >>> electrode3
> > >>> electrode1
> > >>> electrode4
> > >>> electrode2
> > >>> electrode1
> > >>> electrode2
> > >>> electrode3
> > >>> electrode2
> > >>> electrode4
> > >>> electrode3
> > >>> electrode1
> > >>> electrode3
> > >>> electrode2
> > >>> electrode3
> > >>> electrode4
> > >>> electrode4
> > >>> electrode1
> > >>> electrode4
> > >>> electrode2
> > >>> electrode4
> > >>> electrode3
> > >>>
> > >>>
> > >>>
> > >>>
> > >>>
> > >>>
> > >>>> Gesendet: Dienstag, 23. Juli 2013 um 13:36 Uhr
> > >>>> Von: "Rui Barradas" <ruipbarradas at sapo.pt>
> > >>>> An: dennis1991 at gmx.net
> > >>>> Cc: smartpink111 at yahoo.com, 'r-help' <r-help at r-project.org>
> > >>>> Betreff: Re: Aw: Re: [R] How to split two levels several times?
> > >>>>
> > >>>> Hello,
> > >>>>
> > >>>> It's better if you keep this on the list, the odds of getting more and
> > >>>> better answers are greater.
> > >>>>
> > >>>> As for your new question, try the following.
> > >>>>
> > >>>>
> > >>>> 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)])))
> > >>>> split(XXX, idx)
> > >>>>
> > >>>>
> > >>>> Hope this helps,
> > >>>>
> > >>>> Rui Barradas
> > >>>>
> > >>>> Em 23-07-2013 11:41, dennis1991 at gmx.net escreveu:
> > >>>>> Hi
> > >>>>> this type of splitting works for my specific example. Thanks for your help.
> > >>>>>
> > >>>>> I was not absolutely clear what I generally want. I'm looking for an option that generally permits splitting two joint levels of a table after each other. For instance for the table below I want it to be divided into combinations electrode1-electrode2, electrode3-electrode2, electrode4-electrode1. How should I split this?
> > >>>>>
> > >>>>>
> > >>>>> This is the table "XXX"
> > >>>>>
> > >>>>> electrode length
> > >>>>>
> > >>>>> electrode1 5.7
> > >>>>> electrode1 6.3
> > >>>>> electrode1 6.2
> > >>>>> electrode2 11.4
> > >>>>> electrode2 9.7
> > >>>>> electrode3 14.2
> > >>>>> electrode3 14.8
> > >>>>> electrode3 12.6
> > >>>>> electrode2 11.4
> > >>>>> electrode2 9.7
> > >>>>> electrode4 17.0
> > >>>>> electrode4 16.3
> > >>>>> electrode4 17.8
> > >>>>> electrode4 18.3
> > >>>>> electrode4 16.9
> > >>>>> electrode4 18.5
> > >>>>> electrode1 5.7
> > >>>>> electrode1 6.3
> > >>>>> electrode1 6.2
> > >>>>>
> > >>>>>
> > >>>>>
> > >>>>>
> > >>>>>
> > >>>>>> Gesendet: Montag, 22. Juli 2013 um 17:53 Uhr
> > >>>>>> Von: "Rui Barradas" <ruipbarradas at sapo.pt>
> > >>>>>> An: dennis1991 at gmx.net
> > >>>>>> Cc: r-help at r-project.org
> > >>>>>> Betreff: Re: [R] How to split two levels several times?
> > >>>>>>
> > >>>>>> Hello,
> > >>>>>>
> > >>>>>> Sorry, I've just realized that your data frame is named 'XXX', not
> > >>>>>> 'dat'. Change that and the rest should work:
> > >>>>>>
> > >>>>>>
> > >>>>>> idx <- cumsum(c(TRUE, diff(XXX$electrode == "electrode1") > 0))
> > >>>>>> split(XXX, idx)
> > >>>>>>
> > >>>>>>
> > >>>>>> Rui Barradas
> > >>>>>>
> > >>>>>> Em 22-07-2013 16:47, Rui Barradas escreveu:
> > >>>>>>> Hello,
> > >>>>>>>
> > >>>>>>> Try the following.
> > >>>>>>>
> > >>>>>>>
> > >>>>>>> idx <- cumsum(c(TRUE, diff(dat$electrode == "electrode1") > 0))
> > >>>>>>> split(dat, idx)
> > >>>>>>>
> > >>>>>>>
> > >>>>>>> Hope this helps,
> > >>>>>>>
> > >>>>>>> Rui Barradas
> > >>>>>>>
> > >>>>>>> Em 22-07-2013 15:09, dennis1991 at gmx.net escreveu:
> > >>>>>>>> Hi,
> > >>>>>>>>
> > >>>>>>>> I have a small problem with the function split() and would appreciate
> > >>>>>>>> your help.
> > >>>>>>>>
> > >>>>>>>> I have a table called “XXX” with 2 columns and 49 rows. The 49 rows
> > >>>>>>>> belong to 8 different levels (electrode1, ...,electrode8). I want to
> > >>>>>>>> split the table always at the row where “electrode1” starts again so
> > >>>>>>>> that I can export 7 individual dataframes (numbered “dataframe1” to
> > >>>>>>>> ”dataframe7”) which contain always electrode1 as first level (always
> > >>>>>>>> three rows) with the varying number of rows for electrodes2-8 below.
> > >>>>>>>> I tried the split function with various setups:
> > >>>>>>>>
> > >>>>>>>> t <- as.factor(XXX$electrode)
> > >>>>>>>>
> > >>>>>>>> dataframeX <- split(XXX, f=(levels=t))
> > >>>>>>>>
> > >>>>>>>> But this doesn’t work. Could you please help. Thank you! Dennis
> > >>>>>>>>
> > >>>>>>>>
> > >>>>>>>> This is the table "XXX"
> > >>>>>>>>
> > >>>>>>>> electrode length
> > >>>>>>>>
> > >>>>>>>> electrode1 5.7
> > >>>>>>>> electrode1 6.3
> > >>>>>>>> electrode1 6.2
> > >>>>>>>> electrode2 11.4
> > >>>>>>>> electrode2 9.7
> > >>>>>>>> electrode1 5.7
> > >>>>>>>> electrode1 6.3
> > >>>>>>>> electrode1 6.2
> > >>>>>>>> electrode3 14.2
> > >>>>>>>> electrode3 14.8
> > >>>>>>>> electrode3 12.6
> > >>>>>>>> electrode1 5.7
> > >>>>>>>> electrode1 6.3
> > >>>>>>>> electrode1 6.2
> > >>>>>>>> electrode4 17.0
> > >>>>>>>> electrode4 16.3
> > >>>>>>>> electrode4 17.8
> > >>>>>>>> electrode4 18.3
> > >>>>>>>> electrode4 16.9
> > >>>>>>>> electrode4 18.5
> > >>>>>>>> electrode1 ....
> > >>>>>>>> .... ....
> > >>>>>>>> electrode5 ....
> > >>>>>>>> .... ....
> > >>>>>>>> electrode1 ....
> > >>>>>>>> electrode6 ....
> > >>>>>>>> electrode1 ....
> > >>>>>>>> electrode7 ....
> > >>>>>>>> electrode1 ....
> > >>>>>>>> electrode8 ....
> > >>>>>>>>
> > >>>>>>>> ______________________________________________
> > >>>>>>>> R-help at r-project.org mailing list
> > >>>>>>>> https://stat.ethz.ch/mailman/listinfo/r-help
> > >>>>>>>> PLEASE do read the posting guide
> > >>>>>>>> http://www.R-project.org/posting-guide.html
> > >>>>>>>> and provide commented, minimal, self-contained, reproducible code.
> > >>>>>>>>
> > >>>>>>>
> > >>>>>>> ______________________________________________
> > >>>>>>> R-help at r-project.org mailing list
> > >>>>>>> https://stat.ethz.ch/mailman/listinfo/r-help
> > >>>>>>> PLEASE do read the posting guide
> > >>>>>>> http://www.R-project.org/posting-guide.html
> > >>>>>>> and provide commented, minimal, self-contained, reproducible code.
> > >>>>>>
> > >>>>
> > >>
> >
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>
More information about the R-help
mailing list