1

For some reason, it is not clear why my code for row col extraction is not working. This sample code should work.

set.seed(42)
FX <- data.frame(Location=c(5, 7:10), mi=c(1, 34.6, 6, 44, 5.55))
RX <- data.frame(Course=runif(n=1, min=4, max=5), Cor=runif(n=1, min=2, max=3))
FX
#   Location    mi
# 1        5  1.00
# 2        7 34.60
# 3        8  6.00
# 4        9 44.00
# 5       10  5.55
RX
#     Course      Cor
# 1 4.914806 2.937075

Current code that is not working:

which(FX==RX[1,1], arr.ind=TRUE)

Desired output:

Row and all cols

dcarlson
  • 10,936
  • 2
  • 15
  • 18
Toy L
  • 55
  • 6
  • 1
    Your example shows length difference – akrun Jul 29 '21 at 21:08
  • Is this [R FAQ 7.31](https://cran.r-project.org/doc/FAQ/R-FAQ.html#Why-doesn_0027t-R-think-these-numbers-are-equal_003f)? Also [Why are these numbers not equal?](https://stackoverflow.com/questions/9508518/why-are-these-numbers-not-equal). – Rui Barradas Jul 29 '21 at 21:53
  • There are no values in `FX` which is equal to `RX[1,1]`. What is your expected output? – Ronak Shah Jul 30 '21 at 02:40
  • The expected output should be a value closest to it. – Toy L Aug 03 '21 at 03:38

1 Answers1

0

'RX' is just having a single row, We may need to replicate it to make the dimensions same

FX == RX[col(FX)]

Also, these are float values, so there is some precision involved i.e. it may not be exactly equal unless we do some rounding

akrun
  • 874,273
  • 37
  • 540
  • 662
  • 1
    Once the code is fixed and `RX` contains a value in the data, the original code works, e.g. `RX[1, 1] <- 5.55; ; which(FX==RX[1,1], arr.ind=TRUE)` returns row 5, col 2. Not rounding will certainly lead to errors, e.g. `which(round(FX, 2)==round(RX[1,1], 2), arr.ind=TRUE)`. – dcarlson Jul 29 '21 at 21:28
  • @akrun, why must the data frames be equal? – Toy L Aug 01 '21 at 05:20
  • @ dcarlson, What is `RX[1, 1] <- 5.55` meant to do? is it an objective or is `" ; ;"`mean to be apart of the line of code as well? – Toy L Aug 01 '21 at 05:22
  • 1
    @ToyL It is because of the property of `==` i.e. it is an elementwise operator. So, it needs the length to be same on lhs and rhs – akrun Aug 01 '21 at 18:38
  • Can't ` test_1<-abs(FX-RX[1,1])` `result0<-which(test_1==min(test_1),arr.ind=TRUE)`be an alternative to using `rounding` and finding the same result ie row 5, col2? – Toy L Aug 03 '21 at 04:07
  • My concern with the `round()` operator is that with digits longer than 2, I may get multiple outputs. – Toy L Aug 03 '21 at 04:10