In a function with three arguments (i.e. state, outcome and nums) and the data frame is called df. Here is the snippet with the issue:
else if (num == "worst"){
da <- as.numeric(unlist(df[,outcome]))
da <- na.omit(da)
da <- sort(da, decreasing = T)
dm <- match(da, df[,outcome])
print(unique(df[dm, "hospital"]))
}
the values from da printed separately are as follows:
[1] 19.0 18.4 17.6 17.3 17.3 17.1 17.1 16.8 16.8 16.7 16.6 16.5 16.5 16.4 16.3 16.3 16.3 16.2 16.2 16.2 16.2 16.2 16.2
[24] 16.2 16.2 16.0 16.0 15.9 15.8 15.8 15.8 15.8 15.8 15.7 15.6 15.6 15.6 15.5 15.4 15.4 15.4 15.3 15.3 15.3 15.3 15.2
[47] 15.2 15.2 15.1 15.1 15.0 15.0 15.0 14.9 14.9 14.9 14.9 14.8 14.7 14.7 14.7 14.7 14.7 14.6 14.5 14.5 14.3 14.3 14.3
[70] 14.2 14.2 14.1 14.1 14.1 14.0 14.0 13.8 13.8 13.6 13.5 13.5 13.3 13.2
these values are directly derived from the original df data frame.
when trying to match the values to extract the indexes using dm variable, here is what is returned:
[1] NA 41 88 14 14 52 52 45 45 58 34 26 26 16 8 8 8 3 3 3 3 3 3 3 3 NA NA 12 36 36 36 36 36 33 9 9 9 60 13
[40] 13 13 53 53 53 53 44 44 44 4 4 NA NA NA 5 5 5 5 85 7 7 7 7 7 89 2 2 38 38 38 18 18 30 30 30 NA NA 71 71
[79] 48 23 23 86 1
the issue: for example, the first value of 19.0 from da is not recognized in dm, even though that value is derived from the same database of df. Can someone explain me what the issue is and how i can identify the missing variables to retrieve the index values to print specific rows based on the index values.
NOTE
I have already tried to switch to which(x %in% y)
method:
da <- sort(da, decreasing = T)
dm <- which(df[,outcome] %in% da)
but that just gives this as an output:
[1] 1 2 3 4 5 7 8 9 10 12 13 14 15 16 17 18 20 22 23 24 25 26 27 30 31 32 33 34 35
[30] 36 37 38 39 40 41 43 44 45 46 47 48 49 52 53 55 56 57 58 59 60 61 62 63 64 65 66 68 70
[59] 71 72 73 74 75 77 79 81 83 84 85 86 88 89 91 105 111
Thanks in advance for your help.
dput link https://pastelink.net/2e8tm