Hi,
Try:
b1<- b

b1[!b1>=7]<- NA

lst1 <- split(b1,cumsum(c(0,abs(diff(b>=7)))))
 indx <- as.logical(((seq_along(lst1)-1)%%2))


lst1[indx]<- lapply(seq_along(lst1[indx]),function(i) {lst1[indx][[i]]<- 
rep(i,length(lst1[indx][[i]]))})
 C2 <- unlist(lst1,use.names=FALSE)

 all.equal(c1,C2)
#[1] TRUE


A.K.

----- Original Message -----
From: arun <smartpink...@yahoo.com>
To: Benjamin Gillespie <gy...@leeds.ac.uk>
Cc: 
Sent: Wednesday, October 9, 2013 8:19 PM
Subject: Re: [R] Possible loop/ if statement query

Hi,

There should be a simpler way with cumsum(diff()).


b=c((1:10),sort(1:9,decreasing=TRUE),(2:12),sort(6:11,decreasing=TRUE),(7:13))
b1<- b
c1=c( 
NA,NA,NA,NA,NA,NA,1,1,1,1,1,1,1,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,2,2,2,2,2,2,2,2,2,2,2,NA,3,3,3,3,3,3,3)
 

 
 rl1<- rle(b1>=7)
 indx1<-(cumsum(rl1$lengths)+1)[!rl1$values]
 indx2<-(cumsum(rl1$lengths))[rl1$values]
b1[!b1>=7] <- NA

 lst1<- split(sort(c(indx1,indx2)),((seq_along(sort(c(indx1,indx2)))-1)%/%2)+1)
mat1<- sapply(seq_along(lst1),function(i) {x<- lst1[[i]];  b1[seq(x[1],x[2])]<- 
i; b1  })

indx2New<- !is.na(mat1[,2]) & mat1[,2]==2
 indx3New<- !is.na(mat1[,3]) & mat1[,3]==3
 mat1[!is.na(mat1[,1]) & mat1[,1]>3,1] <- 
c(mat1[,2][indx2New],mat1[,3][indx3New])
 all.equal(c1,mat1[,1])
#[1] TRUE


A.K.





----- Original Message -----
From: Benjamin Gillespie <gy...@leeds.ac.uk>
To: "r-help@R-project.org" <r-help@r-project.org>
Cc: 
Sent: Wednesday, October 9, 2013 6:39 PM
Subject: [R] Possible loop/ if statement query

Dear r genii,

I hope you can help.

I have vector 'b':

b=c((1:10),sort(1:9,decreasing=TRUE),(2:12),sort(6:11,decreasing=TRUE),(7:13))

and, from 'b' I wish to create vector 'c':

c=c(    
NA,NA,NA,NA,NA,NA,1,1,1,1,1,1,1,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,NA,2,2,2,2,2,2,2,2,2,2,2,NA,3,3,3,3,3,3,3)

The rules I want to use to create 'c' are:

A numeric of equal to, or over 7 in 'b' needs to result in a numeric (i.e. not 
NA) in 'c';
A numeric of less than 7 in 'b' needs to result in "NA" in 'c';
Where 'groups' of numerics equal to, or over 7 in 'b' are present (i.e. next to 
each other in the list), the numerics produced in 'c' all need to be the same;
Each 'group' of numerics in 'b' must result in a unique numeric  in 'c' (and, 
ideally, they should run in sequence as in 'c' above (1,2,3...).

If anyone has any idea where to start on this or can crack it I'll be most 
grateful!!

Many thanks in advance, 

Ben Gillespie, Research Postgraduate
o-------------------------------------------------------------------o
School of Geography, University of Leeds, Leeds, LS2 9JT
o-------------------------------------------------------------------o
Tel: +44(0)113 34 33345
Mob: +44(0)770 868 7641
o-------------------------------o
http://www.geog.leeds.ac.uk/
o-------------------------------------o
@RiversBenG
o--------------o
______________________________________________
R-help@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.


______________________________________________
R-help@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.

Reply via email to