6
I have a vector I want to order using sort
but, in doing so, I do not see the missing values (NA
). How to do it? Grateful.
> x
[1] "b" "c" "a" NA NA "b" "c" "a" NA NA "b" "c" "a"
> sort(x)
[1] "a" "a" "a" "b" "b" "b" "c" "c" "c"
6
I have a vector I want to order using sort
but, in doing so, I do not see the missing values (NA
). How to do it? Grateful.
> x
[1] "b" "c" "a" NA NA "b" "c" "a" NA NA "b" "c" "a"
> sort(x)
[1] "a" "a" "a" "b" "b" "b" "c" "c" "c"
6
Just do it this way, using na.last=FALSE
for the missing values to appear at the beginning, na.last=TRUE
to emerge at the end, when na.last=NA
is the default value of this parameter:
> x
[1] "b" "c" "a" NA NA "b" "c" "a" NA NA "b" "c" "a"
> sort(x, na.last = FALSE)
[1] NA NA NA NA "a" "a" "a" "b" "b" "b" "c" "c" "c"
> sort(x, na.last = TRUE)
[1] "a" "a" "a" "b" "b" "b" "c" "c" "c" NA NA NA NA
> sort(x, na.last = NA)
[1] "a" "a" "a" "b" "b" "b" "c" "c" "c"
3
If you use the function order
, the standard is na.last = T
.
So this way it would work:
> x <- c("c", "a", NA, "b")
> x[order(x)]
[1] "a" "b" "c" NA
Just like in Alexandre’s answer, you can put the NA’s in front using:
> x[order(x, na.last = F)]
[1] NA "a" "b" "c"
I believe @Alexandre’s answer is better, this is just another way to do!
Browser other questions tagged r classification
You are not signed in. Login or sign up in order to post.