[R] I think a simple question

Gavin Simpson gavin.simpson at ucl.ac.uk
Sun Nov 12 23:45:13 CET 2006


On Sun, 2006-11-12 at 17:20 -0500, Leeds, Mark (IED) wrote:
> I have index ( of a vector ) values of say
> 
> tempin<-c(1 31 61   91 121     all the way upto 1411)
> 
> What I want is a function that takes in a number say, x = 5, and gives
> me an new vector
> of
> 
> tempout<-1  6 31  36 91  96  121  126  .......... 1411   1416
> 
> This can't be so hard but I can't get it and I've honestly tried.
> Obviously, tempin + 5 gives me the missing values but I don't know how
> to interwine them in the order above. Thanks for any help
> you can provide.
> 
>                                                                mark

Hi Mark,

?sort, as in, use sort on the concatenated vector of data and (data  +
5):

## dummy data
> dat <- floor(seq(1, 1411, length = 20))
> dat
 [1]    1   75  149  223  297  372  446  520  594  668  743
[12]  817  891  965 1039 1114 1188 1262 1336 1411
## simply sort dat and dat + 5, concatenated together
> sort(c(dat, dat + 5))
 [1]    1    6   75   80  149  154  223  228  297  302  372
[12]  377  446  451  520  525  594  599  668  673  743  748
[23]  817  822  891  896  965  970 1039 1044 1114 1119 1188
[34] 1193 1262 1267 1336 1341 1411 1416

which if you want a function, a suitable wrapper would be:

foo <- function(x, inc) {
	sort(c(x, x + inc))
}

> foo(dat, 5)
 [1]    1    6   75   80  149  154  223  228  297  302  372
[12]  377  446  451  520  525  594  599  668  673  743  748
[23]  817  822  891  896  965  970 1039 1044 1114 1119 1188
[34] 1193 1262 1267 1336 1341 1411 1416

Of course, checking for suitability of input data for foo is left up to
you.

HTH

G

-- 
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%
Gavin Simpson                     [t] +44 (0)20 7679 0522
ECRC                              [f] +44 (0)20 7679 0565
UCL Department of Geography
Pearson Building                  [e] gavin.simpsonATNOSPAMucl.ac.uk
Gower Street
London, UK                        [w] http://www.ucl.ac.uk/~ucfagls/
WC1E 6BT                          [w] http://www.freshwaters.org.uk/
%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%~%



More information about the R-help mailing list