[R] I think a simple question

Gavin Simpson gavin.simpson at ucl.ac.uk
Mon Nov 13 00:04:08 CET 2006


On Sun, 2006-11-12 at 22:45 +0000, Gavin Simpson wrote:
> 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):

Whoops, my solution of course works fine if the increment (x = 5) is
small compared to the difference between values in tempin, but it fails
miserably with something like an increment of 100:

> 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
> foo <- function(x, inc) { sort(c(x, x+inc))}
> foo(dat, 100)
 [1]    1   75  101  149  175  223  249  297  323  372  397
[12]  446  472  520  546  594  620  668  694  743  768  817
[23]  843  891  917  965  991 1039 1065 1114 1139 1188 1214
[34] 1262 1288 1336 1362 1411 1436 1511

An alternative solution that seems to work is

> bar <- function(x, inc)
{
	len <- length(x) * 2
	retval <- numeric(length = len)
	## identify odd/even entries
	inds <- as.logical(1:len %% 2)
	## fill the odds (original)
	retval[inds] <- x
	## fill the evens (incremented)
	retval[!inds] <- x + inc
	retval
}
> bar(dat, 100)
 [1]    1  101   75  175  149  249  223  323  297  397  372
[12]  472  446  546  520  620  594  694  668  768  743  843
[23]  817  917  891  991  965 1065 1039 1139 1114 1214 1188
[34] 1288 1262 1362 1336 1436 1411 1511

HTH

G

> 
> ## 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