I'm trying to do sorting for something like this:
State Value
AK 1
WA 3
LA NA
AK NA
OR 1
DL 1
and then try to find say the state of rank 2.
If I sort by the order of "Value", it'd become something like this:
State Value
AK 1
OR 1
DL 1
WA 3
LA NA
AK NA
But then this is not the ranking I want. I want to sort it alphabetically, which means rank 2 should be DL instead of OR.
The way I do it is I use a while loop to check if there are states above and below OR with the same Value, and get all the states with same Value. Take them out and reorder them and then map their positions.
count <- 2
ucount <- 0
dcount <- count
while (data$Value[count] == data$Value[count + 1]) {
dcount <- dcount + 1
count <- dcount
}
count <- num
while (data$Value[count] == data$Value[count - 1]) {
ucount <- ucount + 1
count <- count - 1
}
if (dcount == 2 && ucount == 0) {
result <- as.character(data$State[2])
}
else if (dcount != num) {
result <- as.character(sort(data[ucount:dcount, "State"])[1])
}
else {
result <- as.character(sort(data[ucount:dcount, "State"])[ucount + 1])
}
result
This code, tho ugly, works, but only when there's no NA values on the below.
If we don't have
State Value
WA 3
The data frame would become
State Value
AK 1
OR 1
DL 1
LA NA
AK NA
Then the code won't work because it'll try to compare with the value of LA, which is NA. I want to tell the while loop to stop when it see an NA but R doesn't seem to let me do anything with NA.
I know this is probably not a very smart to do it but it's the only way I can think of. I'm still pretty new to R, and I hope this is not a stupid question. Thanks for your help!
PS I've checked this post How to sort a dataframe by column(s)?, which is indeed similar to what I wanted to do. However, my question is about how to deal with NAs. I'm happy theres an alternative way to my approach (which I know is not very smart, haha) that can solve my problem nicely, but I'm still hoping with this question I can see some insights about NAs.
The function
order()
accepts multiple arguments, allowing you to sort first onValue
and then -- for breaking any ties -- by the alphabetical order ofState
: