[R] Fwd: request: most repeated sequnce
jim holtman
jholtman at gmail.com
Sun Sep 7 17:43:45 CEST 2008
---------- Forwarded message ----------
From: jim holtman <jholtman at gmail.com>
Date: Sun, Sep 7, 2008 at 11:42 AM
Subject: Re: [R] request: most repeated sequnce
To: Muhammad Azam <mazam72 at yahoo.com>
This should do it for you:
> 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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
> x=array(x,dim=c(3,6,7))
> apply(x,3,function(.mat){
+
+ rows <- table(apply(.mat,1,function(z){
+ # remove the zeros
+ z <- z[z != 0]
+
+ paste(z,collapse=' ')
+ }))
+ # remove empty strings
+ rows <- rows[names(rows) != ""]
+
+ if (!is.null(rows)){
+ return(names(rows)[which.max(rows)])
+ } else return(NULL)
+ })
[[1]]
[1] "1"
[[2]]
[1] "1 2 3"
[[3]]
[1] "1 2 3 4"
[[4]]
[1] "1 2 3 4"
[[5]]
[1] "2 2 3 4"
[[6]]
character(0)
[[7]]
[1] "1"
>
On Sun, Sep 7, 2008 at 8:08 AM, Muhammad Azam <mazam72 at yahoo.com> wrote:
> Dear Jim Holtman
> Thanks a lot for your help. The problem is still there. Please consider this
> set of values
>
> 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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)
> x=array(x,dim=c(3,6,7))
> apply(x,3,function(.mat){
>
> rows <- table(apply(.mat,1,function(z){
> # remove the zeros
> z <- z[z != 0]
> if (length(z) == 0) return(NULL)
> paste(z,collapse=' ')
> }))
> names(rows[which.max(rows)])
> })
>
> output is:
> Error in as.vector(x, mode) : invalid argument 'mode'
>
>
> Note: the obtained rows consist of all zeros should not take part in most
> repeated sequence process.
>
> best regards
> Muhammad Azam
>
> ----- Original Message ----
> From: jim holtman <jholtman at gmail.com>
> To: Muhammad Azam <mazam72 at yahoo.com>
> Cc: R-help request <r-help-request at r-project.org>; R Help
> <r-help at r-project.org>
> Sent: Sunday, September 7, 2008 12:36:18 AM
> Subject: Re: [R] request: most repeated sequnce
>
> This may come closer since it removes the zeros before comparison:
>
>>
>> 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){
> + # remove the zeros
> + z <- z[z != 0]
> + if (length(z) == 0) return(NULL)
> + paste(z,collapse=' ')
> + }))
> + names(rows[which.max(rows)])
> + })
> [1] "1" "1 2 3" "1 2 3 4" "1 2 3 4" "2 2 3 4"
>>
>>
>>
>
>
> On Sat, Sep 6, 2008 at 12:48 PM, Muhammad Azam <mazam72 at yahoo.com> wrote:
>> Dear R community
>> Initially i thought my problem has been solved but one thing which i found
>> e.g. if
>> 1. All the elements of a sector are zero e.g
>> , , 7
>>
>> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
>> [1,] 0 0 0 0 0 0 0 0 0 0
>> [2,] 0 0 0 0 0 0 0 0 0 0
>> [3,] 0 0 0 0 0 0 0 0 0 0
>> [4,] 0 0 0 0 0 0 0 0 0 0
>> [5,] 0 0 0 0 0 0 0 0 0 0
>>
>> 2. Majority of the rows consist of zeros e.g.
>> , , 5
>>
>> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
>> [1,] 4 4 0 0 0 0 0 0 0 0
>> [2,] 4 4 0 0 0 0 0 0 0 0
>> [3,] 0 0 0 0 0 0 0 0 0 0
>> [4,] 0 0 0 0 0 0 0 0 0 0
>> [5,] 0 0 0 0 0 0 0 0 0 0
>>
>> Actually
>> zeros are not my values. I get values and fill the remaining parts with
>> zeros like "x=array(0,dim=c(3,6,5))". Now according to first strategy
>> "0 0 0 0 0 0 0 0 0 0" are most repeated
>> sequence of rows in both of above cases. But i don't want to consider
>> cases where all elements are zeros and interested to get "4 4
>> 0 0 0 0 0 0 0 0" or just " 4 4 " in case 2.
>> Thanks and best regards
>>
>> Muhammad Azam
>>
>>
>>
>>
>>
>> ----- Original Message ----
>> From: jim holtman <jholtman at gmail.com>
>> To: Muhammad Azam <mazam72 at yahoo.com>
>> Cc: R Help <r-help at r-project.org>; R-help request
>> <r-help-request at r-project.org>
>> Sent: Saturday, September 6, 2008 2:39:19 PM
>> Subject: Re: [R] request: most repeated sequnce
>>
>> 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?
>>
>>
>>
>>
>> [[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?
>
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem that you are trying to solve?
--
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