[R] re : array searches

Olivier ETERRADOSSI olivier.eterradossi at ema.fr
Fri Feb 16 16:02:51 CET 2007


Hi,
I am not sure to get the issue, but assuming your data are arranged as 
in your example with dates in column x$V1 and signals as x$V2 ,
I think that you could use "rle" in the following way :

test<-rle(x$V2)
testmat<-matrix(NA,length(test$values),2)
testmat[,1]<-x[c(1,cumsum(test$length)[1:(length(test$values)-1)]+1),1]
testmat[,2]<-test$values

And you'll get your result without the duplicates, arranged as follow :
     [,1]         [,2]
[1,] "31/01/2007" "-1"
[2,] "03/02/2007" "1"
[3,] "09/02/2007" "0"
[4,] "12/02/2007" "1"
[5,] "15/02/2007" "0"

Is that what you want or did I miss it ?!
Hope this helps. Olivier



 
> essage: 92
> Date: Fri, 16 Feb 2007 10:14:30 +0000
> From: "Murali Menon" <feanor0 at hotmail.com>
> Subject: [R] array searches
> To: r-help at stat.math.ethz.ch
> Message-ID: <BAY113-F191B663BED1069C6C1EDE9EE950 at phx.gbl>
> Content-Type: text/plain; format=flowed
>
> Folks,
>
> I have a dataframe comprising a column of dates and a column of signals (-1, 
> 0, 1) that looks something like this:
>
> 30/01/2007	0
> 31/01/2007	-1
> 01/02/2007	-1
> 02/02/2007	-1
> 03/02/2007	1
> 04/02/2007	1
> 05/02/2007	1
> 06/02/2007	1
> 07/02/2007	1
> 08/02/2007	1
> 09/02/2007	0
> 10/02/2007	0
> 11/02/2007	0
> 12/02/2007	1
> 13/02/2007	1
> 14/02/2007	1
> 15/02/2007	0
> 16/02/2007	0
>
> What I need to do is for each signal *in reverse chronological order* to 
> find the date that it first appeared. So, for the zero on 16/02/2007 and 
> 15/02/2007, the 'inception' date would be 15/02/2007, because the day 
> before, the signal was 1. Likewise, the 'inception' date for the signal 1 on 
> 08/02/2007 and the five days prior, would be 03/02/2007. I need to create a 
> structure of inception dates that would finally look as follows:
>
> -1	31/01/2007
> -1	31/01/2007
> -1	31/01/2007
> 1	03/02/2007
> 1	03/02/2007
> 1	03/02/2007
> 1	03/02/2007
> 1	03/02/2007
> 1	03/02/2007
> 0	09/02/2007
> 0	09/02/2007
> 0	09/02/2007
> 1	12/02/2007
> 1	12/02/2007
> 1	12/02/2007
> 0	15/02/2007
> 0	15/02/2007
>
> Is there a clever way of doing this? My sadly C-oriented upbringing can only 
> think in terms of for-loops.
>
> Thanks!
>
> Murali

-- 
Olivier ETERRADOSSI
Maître-Assistant
CMGD / Equipe "Propriétés Psycho-Sensorielles des Matériaux"
Ecole des Mines d'Alès
Hélioparc, 2 av. P. Angot, F-64053 PAU CEDEX 9
tel std: +33 (0)5.59.30.54.25
tel direct: +33 (0)5.59.30.90.35 
fax: +33 (0)5.59.30.63.68
http://www.ema.fr



More information about the R-help mailing list