简体   繁体   中英

'which' function in R returns row = 1 while the matched value is in row 2?

I have a translation table where I am using do.call(paste, input) %in% do.call(paste, big_translation_table) .

It returns TRUE or FALSE .

Then I am using which function to find the index, but it always returns 1 .

Here is a small example:

test1 <- data.frame(a = 1, b=2, c = "r", stringsAsFactors = FALSE)
test2 <- data.frame(a = c(1,2), b=c(2,10), c = c("r","p"), stringsAsFactors = FALSE)

which(do.call(paste, test1) %in% do.call(paste, test2))

returns 1 and it's ok, now let's test with:

test1 <- data.frame(a = 2, b=10, c = "p", stringsAsFactors = FALSE)
which(do.call(paste, test1) %in% do.call(paste, test2))

returns 1 too. I think it should return 2.

%in% is just a logical test of whether (in this case) test1 appears in test2 , not on a case by case basis. I think you just want == :

test1 <- data.frame(a = 1, b=2, c = "r", stringsAsFactors = FALSE)
> which(do.call(paste, test1) == do.call(paste, test2))
[1] 1

Then:

test1 <- data.frame(a = 2, b=10, c = "p", stringsAsFactors = FALSE)
> which(do.call(paste, test1) == do.call(paste, test2))
[1] 2

The OP has asked to find the row number in the translatation table test2 for matches in all columns. He is pasting all columns together to create a natural key for looking up test1 in test2 .

Instead of repeatedly pasting the columns together it is more efficient to do a join . The data.table package has the which parameter which returns the row numbers:

library(data.table)
setDT(test2)[setDT(test1), on = names(test1), which = TRUE]
 [1] 2

for the 2nd test case.

If it is necessary to be explicit on the columns to be used in the join, we can write

setDT(test2)[setDT(test1), on = .(a, b, c), which = TRUE]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM