[R] split function
Henrique Dallazuanna
wwwhsd at gmail.com
Fri Feb 26 20:49:21 CET 2010
So, I don't understand what you want, you want add the factor to split
result, isn't?
#Example
set.seed(1234)
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
xgD <- split(data.frame(x, g), g)
sapply(xgD, nrow) == table(g)
#
On Fri, Feb 26, 2010 at 4:40 PM, rusers.sh <rusers.sh at gmail.com> wrote:
> Your method seems to only re-express the data "data.frame(x, g)" using
> another format. The results are really from the generated data frame. Maybe
> be not good.
>> table(g)
> g
> 0 1 2 3
> 7 9 8 6
> I hope to randomly split the value 'x' according to the different sample
> sizes of different levels, displayed above. That is, 7 for level 0, 9 for
> level 1, et al.
> Thanks.
> 2010/2/26 Henrique Dallazuanna <wwwhsd at gmail.com>
>>
>> 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
>> > PLEASE do read the posting guide
>> > http://www.R-project.org/posting-guide.html
>> > and provide commented, minimal, self-contained, reproducible code.
>> >
>>
>>
>>
>> --
>> Henrique Dallazuanna
>> Curitiba-Paraná-Brasil
>> 25° 25' 40" S 49° 16' 22" O
>
>
>
> --
> -----------------
> Jane Chang
> Queen's
>
--
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O
More information about the R-help
mailing list