R: match similar values

Ruggero

I have two vectors, let us say:

A <- c(13.25, 14.24, 15.29, 16.27, 17.31)

and

B <- c(10.1, 11.2, 12.1, 13.2, 14.1, 15.3, 16.2, 17.4, 18.3, ...)

and I would like to "match" the values of the first to the second, obtaining the positions of similar values. In this case I would like to obtain

C <- c(4, 5, 6, 7, 8)

I tried with match, but it does not work. Also, I tried match applied to rounded vectors, but the problem is that it does not always work: if B contains 14.48 and C contains 14.51, they are rounded to 14 and 15, respectively...

Pierre L

If you want the nearest value of B to be matched, then you can use which.min with the absolute distance from each element of A:

C <- sapply(A, function(x) which.min(abs(x-B)))
C
[1] 4 5 6 7 8

Updated

With the tolerance, you have to add an extra step to exclude the results out of range:

tolerance <- 0.2
in_range <- unlist(sapply(A, function(x) x[any(abs(x-B) < tolerance)]))
C <- sapply(A, function(x) which.min(abs(x-B)))
C <- C[match(in_range, A)]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

find index of similar values

From Dev

match and count values in sequence by group in R

From Dev

R + Lookup algorithm to match values within a range of identical elements

From Dev

R function similar to Excel's index-match

From Dev

R Match and compare values from different vectors

From Dev

How to match multiple corresponding values between columns in R

From Dev

for loop adding values with similar sign in a numeric vector in R

From Dev

R match key values vector with irregular strings vector

From Dev

r - find list item where all values match a regular expression

From Dev

match / find rows based on multiple required values in a single row in R

From Dev

match with an interval and extract values between two matrix R

From Dev

GetHashCode for similar values

From Dev

R function similar to Excel's index-match

From Dev

warning on if statement to find and replace values using match in R

From Dev

Sum column values that match year in another column in R

From Dev

R - if values match in column A, how often do their corresponding values in column B match?

From Dev

R Match and compare values from different vectors

From Dev

usin NodeList with similar values

From Dev

How to match multiple corresponding values between columns in R

From Dev

R regex match '\' and newline in dataframe to create columns with new values

From Dev

match with an interval and extract values between two matrix R

From Dev

Match similar item in list

From Dev

R- help in creating a matrix for finding similar values

From Dev

Summing values with similar row values

From Dev

How to match similar documents in R

From Dev

R lookup interpolate between values or closest match

From Dev

Group similar values in the array

From Dev

Arranging the column values in R based on similar pairs present in data

From Dev

R: match values in dataframes

Related Related

  1. 1

    find index of similar values

  2. 2

    match and count values in sequence by group in R

  3. 3

    R + Lookup algorithm to match values within a range of identical elements

  4. 4

    R function similar to Excel's index-match

  5. 5

    R Match and compare values from different vectors

  6. 6

    How to match multiple corresponding values between columns in R

  7. 7

    for loop adding values with similar sign in a numeric vector in R

  8. 8

    R match key values vector with irregular strings vector

  9. 9

    r - find list item where all values match a regular expression

  10. 10

    match / find rows based on multiple required values in a single row in R

  11. 11

    match with an interval and extract values between two matrix R

  12. 12

    GetHashCode for similar values

  13. 13

    R function similar to Excel's index-match

  14. 14

    warning on if statement to find and replace values using match in R

  15. 15

    Sum column values that match year in another column in R

  16. 16

    R - if values match in column A, how often do their corresponding values in column B match?

  17. 17

    R Match and compare values from different vectors

  18. 18

    usin NodeList with similar values

  19. 19

    How to match multiple corresponding values between columns in R

  20. 20

    R regex match '\' and newline in dataframe to create columns with new values

  21. 21

    match with an interval and extract values between two matrix R

  22. 22

    Match similar item in list

  23. 23

    R- help in creating a matrix for finding similar values

  24. 24

    Summing values with similar row values

  25. 25

    How to match similar documents in R

  26. 26

    R lookup interpolate between values or closest match

  27. 27

    Group similar values in the array

  28. 28

    Arranging the column values in R based on similar pairs present in data

  29. 29

    R: match values in dataframes

HotTag

Archive