I have a matrix:
A = matrix(1:20,nrow = 5,ncol = 4,byrow = TRUE)
and a vector:
B = c(1,4,11,14,15,20)
I want to get one value from each row under these conditions: if a row of A has multiple matches with B, only take the first match from the row; if a row of A has only one match, just take this matched value; if a row of A has no match, take the column 1 of this row. So the expected result is appended in the last column of A (as data frame).
V1 V2 V3 V4 result
1 1 2 3 4 1
2 5 6 7 8 5
3 9 10 11 12 11
4 13 14 15 16 14
5 17 18 19 20 20
Thank you for any helps!
i1 <- max.col(array(A %in% B, dim(A)), 'first')
data.frame(A, result = A[cbind(seq_len(nrow(A)), i1)])
X1 X2 X3 X4 result
1 1 2 3 4 1
2 5 6 7 8 5
3 9 10 11 12 11
4 13 14 15 16 14
5 17 18 19 20 20
i2 <- stack(data.frame(t(A)))
cbind(A, aggregate(values~ind, i2, \(x) c(x[x%in%B],x[1])[1])[2])
1 2 3 4 values
1 1 2 3 4 1
2 5 6 7 8 5
3 9 10 11 12 11
4 13 14 15 16 14
5 17 18 19 20 20