[R] How to split two levels several times?
dennis1991 at gmx.net
dennis1991 at gmx.net
Wed Jul 24 14:58:41 CEST 2013
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.
> >>
>
More information about the R-help
mailing list