[R] Odp: splitting vector into equal sets
Petr PIKAL
petr.pikal at precheza.cz
Fri Apr 17 08:50:26 CEST 2009
utkarshsinghal <utkarsh.singhal at global-analytics.com> napsal dne
16.04.2009 17:20:10:
> Hi Pikal,
>
> Thanks for your comments and apologies for not providing a clear
example. But
> you have completely ignored the small example I mentioned:
Well, this is not exactly an example. So let us clarify things a bit. You
do not explain how to handle your vector at all.
One option
split(1:20, factor(1:3))
splits your vector to 3 groups. It recycles 1:3 so every third observation
is put into one vector.
This function randomly samples a vector and gives you list with 3 almost
equal groups and can be easily expanded to arbitrary number of groups.
function(x) {
x1<-sample(x, ceiling(length(x)/3))
x.<-x[-x1]
x2<-sample(x., ceiling(length(x.)/2))
x3<-x[-c(x2,x1)]
list(x1,x2,x3)
}
Here are two functions which compute how many members are in each group
and how many are left.
> 20%%3
[1] 2
> 20%/%3
[1] 6
split(x, rep(letters[1:3], each=length(x)%/%3))
and this splits your vector to 3 portions and put last 2 values to the
first vector.
I think I gave you some insights how to handle such problem.
>
> I want to split rnorm(20) into three equal groups.
> Note that here number of observations is not a multiple of number of
groups.
> For that I want an option where I can specify how to treat these extra
which extra observations, first 2, last 2, arbitrary 2?
> observations, i.e., to put these observations into the 1st group or the
last
> group or one in each group starting from 1st or starting from last
Only **you** know how exactly **you** want to handle it so **you** need to
elaborate a function **yourself**. If the function shall split arbitrary
length vector to arbitrary number of groups and put extra values to
arbitrary groups it would not be a simple case and it would need a little
bit of programming work.
> Also I dont want to calculate the number of observations going in each
group beforehand.
You do not need to calculate it yourself. R will do it for you if you tell
it.
Regards
Petr
>
> Regards
> Utkarsh
>
>
> Petr PIKAL wrote:
> Hi
>
> r-help-bounces at r-project.org napsal dne 16.04.2009 15:23:15:
>
>
> Hi R,
>
> I have explored R archives a lot but couldn't find an efficient way of
> doing the following:
>
> I want to split a vector into sets of equal sizes. Is there any inbuilt
> function of doing so with the option of specifying how to treat the
> remaining observations. For example: suppose I want to split 20
> observations in 3 groups, then I also want the option to put the extra
> two observations into the 1st group or the last group or one in each
> group starting from 1st or starting from last.
>
> I have already tried the "cut" & the "quantcut" function but of no use.
> Any help will be appreciated. Note that I am looking for an R function
> rather than lines of code.
>
>
> Did you try split?
> e.g.
> split(rnorm(12), rep(1:3,4))
> gives list with 3 vectors. Then you can use lapply, sapply or other list
> functions to make some computation.
>
> Or I could be completely wrong what you really want as you did not
provide
> any example.
>
> Regards
> Petr
>
>
>
> Regards
> Utkarsh
>
> ______________________________________________
> 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