[R] Possible loop/ if statement query

PIKAL Petr petr.pikal at precheza.cz
Fri Oct 11 15:06:18 CEST 2013


Hi

not sure if it is the most efficient and clever solution

ifelse(b<7, NA, cumsum(c(0,diff(!(b<7)))==1))

Regards
Petr


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Benjamin Gillespie
> Sent: Thursday, October 10, 2013 12:39 AM
> To: r-help at R-project.org
> 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 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