# [R] split function

Henrique Dallazuanna wwwhsd at gmail.com
Fri Feb 26 20:04:42 CET 2010

```Try this:

split(data.frame(x, g), g)

On Fri, Feb 26, 2010 at 3:55 PM, rusers.sh <rusers.sh at gmail.com> wrote:
> Hi,
>  I am using split function and wonder how to add the factor to the splitted
> results.
> #Example
> n <- 3; nn <- 10
> g <- factor(round(n * stats::runif(n * nn)))   #factor
> x <- rnorm(n * nn) + sqrt(as.numeric(g))    #value
> xg <- split(x, g)
> xg
> \$`0`
> [1]  0.82513702 -0.03911584  2.32955347  0.36745335  1.75572642  2.65461438
>  0.41675829
> \$`1`
> [1]  0.8583493  2.4264804 -0.3622378  3.1770015  0.5162129
> \$`2`
>  [1] 1.7914651 1.1440121 0.8097543 1.2064742 1.6411988 1.3743778 1.7094387
> 2.1204501 1.9330132 2.0731997
> [11] 2.8931865 2.5825309 0.6978723
> \$`3`
> [1] 3.0246214 1.6870782 0.9685926 1.6449350 0.9378751
>> g
>  [1] 2 2 3 2 1 3 2 3 3 1 2 2 2 2 0 0 3 0 2 2 1 1 2 2 0 1 2 0 0 0
> Levels: 0 1 2 3
>
>  Anybody can tell me how to add the corresponding values of factor "g" to
> the splitted results 'xg' to get a data frame?
> Something like,
>
> Splitted/xg     factor/g
> 0.82513702        0
> -0.03911584       0
> 2.32955347        0
>    ...
>  I know i can use "xg\$'0',xg\$'1',xg\$'2',xg\$'3'" to get the values of each
> class and then add a new variable to indicate the factor.
> But i hope to get a method to automatic do those things. Any ideas?
>  Thanks.
>
>
> --
> -----------------
> Jane Chang
> Queen's
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> and provide commented, minimal, self-contained, reproducible code.
>

--
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O

```