sort rows with missing values in r

To sort rows with missing values in R, you can use the na.last argument of the order() function. Setting na.last = TRUE will sort the missing values to the end of the column, while setting na.last = FALSE will sort them to the beginning.

Here's an example of sorting a data frame called df by a column called col1, where col1 contains missing values represented by NA:

main.r
# create example data
df <- data.frame(col1 = c(1, 2, NA, 4, 5), col2 = c("a", "b", "c", NA, "e"))

# sort by col1 with missing values last
df[order(df$col1, na.last = TRUE), ]
177 chars
6 lines

This will return the sorted data frame with missing values in col1 at the end:

main.r
  col1 col2
1    1    a
2    2    b
4    4 <NA>
5    5    e
3   NA    c
72 chars
7 lines

Alternatively, setting na.last = FALSE will sort the missing values first:

main.r
# sort by col1 with missing values first
df[order(df$col1, na.last = FALSE), ]
79 chars
3 lines

This will return the sorted data frame with missing values in col1 at the beginning:

main.r
  col1 col2
3   NA    c
1    1    a
2    2    b
4    4 <NA>
5    5    e
72 chars
7 lines

related categories

gistlibby LogSnag