input: > values [1] 0.854400 1.648465 1.829830 1.874704 7.670915 7.673585 7.722619 > thresholds [1] 1 3 5 7 9 expected output: [1] 1 4 4 4 7 That is, need a vector of indexes of the maximum value below the threshold. e.g. First element is "1", because value[1] is the largest below threshold "1". Second element is "4", because value[4] is the largest below threshold "3". The way I do it is:> sapply(1:length(threshold), function(x) { length(values[values < threshold[x]])})[1] 1 4 4 4 7 It just seem to me too long and stupid to be like R. Is it already the best way? Somehow I feel which() was designed for a purpose like this, but I couldn't figure out a way to apply which here.
Hi, You could try: val1<- c(0.854400, 1.648465, 1.829830, 1.874704, 7.670915, 7.673585, 7.722619) thresh1<- c(1,3,5,7,9) rowSums(t(replicate(length(thresh1),val1))<= thresh1) #[1] 1 4 4 4 7 #using ?sapply() could be shortened sapply(thresh1,function(x) {sum(val1<x)}) #[1] 1 4 4 4 7 A.K. ----- Original Message ----- From: Zhang Weiwu <zhangweiwu at realss.com> To: r-help at r-project.org Cc: Sent: Friday, September 13, 2013 6:13 AM Subject: [R] how to get values within a threshold input: ??? > values ??? [1] 0.854400 1.648465 1.829830 1.874704 7.670915 7.673585 7.722619 ??? > thresholds ??? [1] 1 3 5 7 9 expected output: ??? [1] 1 4 4 4 7 That is, need a vector of indexes of the maximum value below the threshold. e.g. First? element is "1", because value[1] is the largest below threshold "1". Second element is "4", because value[4] is the largest below threshold "3". The way I do it is:> sapply(1:length(threshold), function(x) { length(values[values < threshold[x]])})[1] 1 4 4 4 7 It just seem to me too long and stupid to be like R. Is it already the best way? Somehow I feel which() was designed for a purpose like this, but I couldn't figure out a way to apply which here. ______________________________________________ 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.
> findInterval(thresholds, values)[1] 1 4 4 4 7 Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf > Of Zhang Weiwu > Sent: Friday, September 13, 2013 3:14 AM > To: r-help at r-project.org > Subject: [R] how to get values within a threshold > > > input: > > > values > [1] 0.854400 1.648465 1.829830 1.874704 7.670915 7.673585 7.722619 > > > thresholds > [1] 1 3 5 7 9 > > expected output: > > [1] 1 4 4 4 7 > > That is, need a vector of indexes of the maximum value below the threshold. > > e.g. > First element is "1", because value[1] is the largest below threshold "1". > Second element is "4", because value[4] is the largest below threshold "3". > > The way I do it is: > > > sapply(1:length(threshold), function(x) { length(values[values < threshold[x]])}) > [1] 1 4 4 4 7 > > It just seem to me too long and stupid to be like R. Is it already the best way? > > Somehow I feel which() was designed for a purpose like this, but I couldn't > figure out a way to apply which here. > > ______________________________________________ > 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.
Possibly Parallel Threads
- on how to make a skip-table
- --cuesheet include the full cue sheet or just the seekponints?
- Intel Boot Agent: PXE-E32: TFTP open timeout for correctly configured tftp-hpa
- samba: offer public share to Windows 98 and writable share to Windows XP
- Strange monitor behavior on forced DVI-D output