[R] Possible loop/ if statement query

Benjamin Gillespie gybrg at leeds.ac.uk
Thu Oct 10 00:39:14 CEST 2013


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


More information about the R-help mailing list