I have two numerics of different length and I need to find a value in the 2nd numeric that is identical with one of the values in the 1st numeric (currently, only one value is identical, but I do not know which). e.g.:
x <- c(15,43,46,76,111,138,205,227,242,330,333,339,348,380,402,403,498,534,579)
y <- c(391, 392, 393, 394, 395, 396, 397, 398, 399, 400, 401, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 416, 417, 418, 419, 420, 421, 422, 423, 424, 425, 426, 427, 428, 429, 430, 431, 432, 433, 434, 435, 436, 437, 438, 439, 440, 441, 442, 443, 444, 445, 446, 447, 448, 449, 450, 451, 452, 453, 454, 455, 456, 457, 458, 459, 460, 461, 462, 463, 464, 465, 466, 467, 469, 470, 471, 472, 473, 474, 475, 476, 477, 478, 479, 480, 481, 482, 483, 484, 485, 486, 487,
488, 489, 490, 491, 492, 493, 494, 495, 496, 497, 498, 499, 500, 501, 503, 504, 505, 506, 507)
My solutions so far failed: I was able to construct a dataframe with the numerics as columns.
df <- dataframe(x=x,y=y)
But:
With which(df$y==15) I can only compare one value at once.
With dplyr::duplicate() I can only find identical values within one column or within the same row.
Currently, I work with just two columns. But it would also be helpful to have code when there are three columns and the goal is to find a value from column 1 in column 2 and 3.
Has anyone an idea?
CodePudding user response:
You can find identical values using intersect
for multiple vectors like this:
Reduce(intersect, list(y,x))
Output:
[1] 498
CodePudding user response:
You can use the %in%
operator to ask which elements of y
are in x
e.g.
which(y %in% x)
#> [1] 105
that gives the index of the element(s) in y
that are also in x
. You can subset with that to find the actual vales:
y[which(y %in% x)]
#> [1] 498
You can then combine %in%
operations with &
if you have more than one vector you want to check.
z <- 498
y[which(y %in% x & y %in% z)]
#> [1] 498
You'll get back an empty vector if there's no matches.
z <- 500
y[which(y %in% x & y %in% z)]
#> numeric(0)