0

There is simple data:

> substr(dataa[35,3:ncol(dataa)],1,7)
 [1] "6.66126" "6.97984" "7.00880" "8.68860" "8.22520" "9.81811" "8.51482" "8.42794" "8.51482" "9.41265"
[11] "9.58642" "9.55746" "0.11613" "0.11584" "0.11555" "0.11353" "0.11150" "0.10947" "0.10860" "0.11526"
[21] "0.11729" "0.12019" "0.11787" "148.395" "150.141" "154.262" "166.020" "155.908" "159.170" "154.377"
[31] "151.901" "158.591" "163.794" "159.351" "166.338" "164.039" "159.787" "160.590" "158.628" "155.936"
[41] "154.261" "154.504" "155.410" "161.253" "163.455" "176.077" "172.55"  "171.85"  "174.676" "169.847"
[51] "172.633" "171.59"  "174.924" "174.293" "167.376" "164.261" "162.985" "161.955" "165.702" "165.4"  
[61] "166.8"   "169.6"   "167.651" "171.631" "169.339" "171.013" "168.543" "164.988" "161.75"  "165.812"
[71] "164.686" "682.230" "692.712" "698.037" "716.197" "735.422"

> as.character(test[[35]])
[1] "682.231" "692.713" "698.037" "716.197" "735.422"

I want to get the result in which place are the test values.

What I did and what happened:

d <- as.character(test[[35]])
which(substr(dataa[35,3:ncol(dataa)],1,7) == d, arr.ind=TRUE)

integer(0)
Warning message:
In substr(dataa[35, 3:ncol(dataa)], 1, 7) == d :
  longer object length is not a multiple of shorter object length

But if I try just one character from d, it works fine:

> which(substr(dataa[35,3:ncol(dataa)],1,7) == "735.422")
[1] 76
ERud
  • 17
  • 5
  • 3
    You need `%in%` rather than `==`, i.e. `which(substr(dataa[35,3:ncol(dataa)],1,7) %in% d, arr.ind=TRUE)` – Allan Cameron Mar 06 '23 at 14:16
  • 1
    Why are you treating numbers as characters? – Sotos Mar 06 '23 at 14:17
  • Have also a look at your question 3 hours ago: [Error when comparing data with function "which" R](https://stackoverflow.com/q/75650045/10488504) – GKi Mar 06 '23 at 14:22
  • @AllanCameron Sure? `d` seems to be a single item, so `==` seems correct. Furthermore, `which(x == y)` is a fairly common pattern, although I would recommend using `match` instead. – Konrad Rudolph Mar 06 '23 at 14:41
  • @KonradRudolph `d` is given as `as.character(test[[35]])`, which appears to be a length-5 character vector, so the `which` is "which of the following members of this length-71 character vector appear in this 5-character vector?". This seems like a perfect use case for `%in%` – Allan Cameron Mar 06 '23 at 14:52
  • @AllanCameron Ah yes, I had overlooked that in OP's input. Still, `which(… %in% …)` is essentially the inverse operation of `match`, and `%in%` calls `match` internally, so this is a fairly convoluted way of calling `match`. – Konrad Rudolph Mar 06 '23 at 15:20

0 Answers0