Finding the minimum positive value

jalapic

I guess I don't know which.min as well as I thought.

I'm trying to find the occurrence in a vector of a minimum value that is positive.

TIME <- c(0.00000,  4.47104,  6.10598,  6.73993,  8.17467,  8.80862, 10.00980, 11.01080, 14.78110, 15.51520, 16.51620, 17.11680)

I want to know for the values z of 1 to 19, the index of the above vector TIME containing the value that is closest to but above z. I tried the following code:

vec <- sapply(seq(1,19,1), function(z) which.min((z-TIME > 0)))
vec 

#[1] 2  2  2  2  3  3  5  5  7  7  8  9  9  9 10 11 12  1  1

To my mind, the last two values of vec should be '12, 12'. The reason it's doing this is because it thinks that '0.0000' is closest to 0.

So, I thought that maybe it was because I exported the data from external software and that 0.0000 wasn't really 0. But,

TIME[1]==0  #TRUE

Then I got further confused. Why do these give the answer of index 1, when really they should be an ERROR?

which.min(0 > 0 ) #1
which.min(-1 > 0 ) #1

I'll be glad to be put right.

EDIT:

I guess in a nutshell, what is the better way to get this result:

#[1] 2  2  2  2  3  3  5  5  7  7  8  9  9  9 10 11 12  12  12

which shows the index of TIME that gives the smallest possible positive value, when subtracting each element of TIME from the values of 1 to 19.

josliber

The natural function to use here (both to limit typing and for efficiency) is actually not which.min + sapply but the cut function, which will determine which range of times each of the values 1:19 falls into:

cut(1:19, breaks=TIME, right=FALSE)
# [1] [0,4.47)    [0,4.47)    [0,4.47)    [0,4.47)    [4.47,6.11) [4.47,6.11) [6.74,8.17)
# [8] [6.74,8.17) [8.81,10)   [8.81,10)   [10,11)     [11,14.8)   [11,14.8)   [11,14.8)  
# [15] [14.8,15.5) [15.5,16.5) [16.5,17.1) <NA>        <NA>       
# 11 Levels: [0,4.47) [4.47,6.11) [6.11,6.74) [6.74,8.17) [8.17,8.81) ... [16.5,17.1)

From this, you can easily determine what you're looking for, which is the index of the smallest element in TIME greater than the cutoff:

(x <- as.numeric(cut(1:19, breaks=TIME, right=FALSE))+1)
# [1]  2  2  2  2  3  3  5  5  7  7  8  9  9  9 10 11 12 NA NA

The last two entries appear as NA because there is no element in TIME that exceeds 18 or 19. If you wanted to replace these with the largest element in TIME, you could do so with replace:

replace(x, is.na(x), length(TIME))
# [1]  2  2  2  2  3  3  5  5  7  7  8  9  9  9 10 11 12 12 12

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Finding minimum value in dictionary

From Dev

Finding minimum value in a collection

From Dev

Finding minimum value according to a comparator

From Dev

Finding a minimum and maximum value in an array

From Dev

finding minimum value between 1 value and a column

From Dev

Finding minimum, maximum value of a list in Python

From Java

Finding the minimum of an array from MAX_VALUE

From Dev

Finding the minimum and maximum value within a Metal texture

From Dev

Finding the minimum value of int numbers in an array (Java)

From Dev

Mongo query - finding minimum value within a document

From Dev

Recursively finding the minimum value in a Binary Tree in Java

From Dev

Finding the minimum value of a nested list and its index

From Dev

loop through lists, finding minimum value greater than previous minimum

From Dev

R - Shift specified columns using minimum value into positive values

From Dev

How to get minimum positive value greater then 0 in php

From Dev

Finding the minimum value of a 2-3-4 tree

From Dev

Finding minimum value using XPath 1.0 does not work

From Dev

Python : Finding an item in a list where a function return a minimum value?

From Dev

finding the minimum value of pair of numbers in array C Lang

From Dev

library header for finding 2 minimum value from array in java

From Dev

finding minimum of a subarray while the value might be only 1 or 2 or 3

From Dev

Finding minimum value in successive continuous time intervals using pandas

From Dev

Finding minimum and maximum value for each row, excluding NaN values

From Dev

Finding a minimum value in each column of 8*720 array?

From Dev

Finding minimum value for each row from 2 matrix R

From Dev

Finding specific positive integer

From Dev

R - Finding minimum values based on multiple conditions and returning one or multiple created strings based on the minimum value

From Dev

finding the second minimum

From Dev

Finding the minimum of composite number

Related Related

  1. 1

    Finding minimum value in dictionary

  2. 2

    Finding minimum value in a collection

  3. 3

    Finding minimum value according to a comparator

  4. 4

    Finding a minimum and maximum value in an array

  5. 5

    finding minimum value between 1 value and a column

  6. 6

    Finding minimum, maximum value of a list in Python

  7. 7

    Finding the minimum of an array from MAX_VALUE

  8. 8

    Finding the minimum and maximum value within a Metal texture

  9. 9

    Finding the minimum value of int numbers in an array (Java)

  10. 10

    Mongo query - finding minimum value within a document

  11. 11

    Recursively finding the minimum value in a Binary Tree in Java

  12. 12

    Finding the minimum value of a nested list and its index

  13. 13

    loop through lists, finding minimum value greater than previous minimum

  14. 14

    R - Shift specified columns using minimum value into positive values

  15. 15

    How to get minimum positive value greater then 0 in php

  16. 16

    Finding the minimum value of a 2-3-4 tree

  17. 17

    Finding minimum value using XPath 1.0 does not work

  18. 18

    Python : Finding an item in a list where a function return a minimum value?

  19. 19

    finding the minimum value of pair of numbers in array C Lang

  20. 20

    library header for finding 2 minimum value from array in java

  21. 21

    finding minimum of a subarray while the value might be only 1 or 2 or 3

  22. 22

    Finding minimum value in successive continuous time intervals using pandas

  23. 23

    Finding minimum and maximum value for each row, excluding NaN values

  24. 24

    Finding a minimum value in each column of 8*720 array?

  25. 25

    Finding minimum value for each row from 2 matrix R

  26. 26

    Finding specific positive integer

  27. 27

    R - Finding minimum values based on multiple conditions and returning one or multiple created strings based on the minimum value

  28. 28

    finding the second minimum

  29. 29

    Finding the minimum of composite number

HotTag

Archive