[R] Selecting values based on two criteria
Boris Steipe
boris.steipe at utoronto.ca
Fri Dec 26 23:22:38 CET 2014
Try this:
d1 <- c(135631,136950,137952,138787,139623,142231,143067,144762,
145601,146441)
d2 <- c(135882,136954,137956,138792,139630,140569,141398,142237,
143078,143907,144771,145611,146446,147285,148128)
len <- length(d1)
# concatenate the two vectors
mrg <- c(d1,d2)
# order the concatenated vector
ord <- order(mrg)
# Look at the result: the indices > len (10) are numbers from d2.
ord # [1] 1 11 2 12 3 13 4 14 5 15 16 17 6 18 7 19 20 8 21 9 22 10 23 24 25
# Thus you are looking for values in ord that are > len,
# and immediately follow a value that is <= len.
# Initialize a result vector (you could also overwrite d1).
d4 <- rep(NULL, len)
# A counting index ...
ind <- 0
# Slowly, element by element. If your vectors are very large you might
# use apply.
for (i in 1:length(ord)) {
if (ord[i] > len && ord[i-1] <= len) {
ind <- ind + 1
d4[ind] <- mrg[ord[i]]
}
}
# your d3:
d3 <- c(135882,136954,137956,138792,139630,142237,143078,144771,
145611,146446)
identical(d3, d4)
# TRUE
# special cases you may need to consider:
# - what if there are values in d1 that are equal to values in d2
# - negative values?
# - what if min(d2) is smaller than than min(d1)?
Cheers,
B.
On Dec 26, 2014, at 4:16 PM, Morway, Eric <emorway at usgs.gov> wrote:
> I'm in need of help selecting from d2 those values that come after a value
> in d1. For example, d2[1] is both greater than d1[1] and is the value that
> is closest to d2[1]. Similarly, d2[2] is both greater than d1[2] and is
> the next "highest" number in d2. Every value in d1 has a corresponding
> value in d2 based on this criteria, however, many of the values in d2 will
> be discarded. The final result I'm looking for is a subset of d2 as given
> in d3. Notice for example that 140569 is discarded and not contained in
> d3. This small example is of course a much smaller example of a much
> larger problem. Example R script of how to whittle down d2 to look like d3
> based on the criteria above
>
> d1 <- c(135631,136950,137952,138787,139623,142231,143067,144762,
> 145601,146441)
>
> d2 <- c(135882,136954,137956,138792,139630,140569,141398,142237,
> 143078,143907,144771,145611,146446,147285,148128)
>
> d3 <- c(135882,136954,137956,138792,139630,142237,143078,144771,
> 145611,146446)
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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