[R] request: most repeated sequnce
jim holtman
jholtman at gmail.com
Sat Sep 6 14:39:19 CEST 2008
Here is a start. You can delete the zeros:
> x=c(1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,2,2,2,3,3,0,0,0,0,0,0,0,0,0,0,1,1,1,2,2,3,3,3,4,4,4,0,0,0,0,0,0,0,1,1,1,2,2,2,3,3,3,4,4,4,
+ 0,0,0,0,0,0,1,2,2,2,2,2,0,3,3,0,4,4,0,0,0,0,0,0)
> x=array(x,dim=c(3,6,5))
> apply(x,3,function(.mat){
+ rows <- table(apply(.mat,1,function(z){
+ paste(z,collapse=' ')
+ }))
+ names(rows[which.max(rows)])
+ })
[1] "1 0 0 0 0 0" "1 2 3 0 0 0" "1 2 3 4 0 0" "1 2 3 4 0 0" "2 2 3 4 0 0"
>
On Sat, Sep 6, 2008 at 4:54 AM, Muhammad Azam <mazam72 at yahoo.com> wrote:
> Dear R community
> Hope every one be in best of his/her health. I have a situation in which there are s-sectors. Each sector is further divided into r-rows and c-columns. All it makes an array having dimension (r,c,s). e.g.
>
> x=c(1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,2,2,2,3,3,0,0,0,0,0,0,0,0,0,0,1,1,1,2,2,3,3,3,4,4,4,0,0,0,0,0,0,0,1,1,1,2,2,2,3,3,3,4,4,4,
> 0,0,0,0,0,0,1,2,2,2,2,2,0,3,3,0,4,4,0,0,0,0,0,0)
> x=array(x,dim=c(3,6,5))
>> x
> , , 1
>
> [,1] [,2] [,3] [,4] [,5] [,6]
> [1,] 1 0 0 0 0 0
> [2,] 1 0 0 0 0 0
> [3,] 1 0 0 0 0 0
>
> , , 2
>
> [,1] [,2] [,3] [,4] [,5] [,6]
> [1,] 1 2 3 0 0 0
> [2,] 1 2 3 0 0 0
> [3,] 1 2 0 0 0 0
>
> , , 3
>
> [,1] [,2] [,3] [,4] [,5] [,6]
> [1,] 1 2 3 4 0 0
> [2,] 1 2 3 4 0 0
> [3,] 1 3 4 0 0 0
>
> , , 4
>
> [,1] [,2] [,3] [,4] [,5] [,6]
> [1,] 1 2 3 4 0 0
> [2,] 1 2 3 4 0 0
> [3,] 1 2 3 4 0 0
>
> , , 5
>
> [,1] [,2] [,3] [,4] [,5] [,6]
> [1,] 1 2 0 0 0 0
> [2,] 2 2 3 4 0 0
> [3,] 2 2 3 4 0 0
>
> I want to get the most repeated sequence (row-wise) of values in each sector. e.g. in sector 1 i.e. , , 1
> the most repeated sequence is 1 (ignoring zeros). In , , 2 the most repeated sequence is 1 2 3. Similarly in last sector i.e.
> , , 5 such sequence is 2 2 3 4. Any body can help to solve this problem. Thanks
>
>
> best regards
> Muhammad Azam
>
>
>
>
> [[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.
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem that you are trying to solve?
More information about the R-help
mailing list