How do I sort a data.frame with one column?
I'm using the following:
> set.seed(456)
> df1 <- data.frame(col1 = runif(10))
> class(df1)
[1] "data.frame"
> df1 <- df1[order(df1$col1),]
> class(df1)
[1] "numeric"
However if I add a blank column things work fine:
> set.seed(456)
> df1 <- data.frame(col1 = runif(10))
> df1$dummy <- NA
> class(df1)
[1] "data.frame"
> df1 <- df1[order(df1$col1),]
> class(df1)
[1] "data.frame"
> df1
col1 dummy
7 0.08243274 NA
1 0.08955160 NA
2 0.21051232 NA
9 0.23750327 NA
8 0.28552695 NA
6 0.33195997 NA
10 0.38523617 NA
3 0.73295527 NA
5 0.78839789 NA
4 0.85213354 NA
Is there a better way to do this?
To sort a data frame in R, use the order( ) function. By default, sorting is ASCENDING. Prepend the sorting variable by a minus sign to indicate DESCENDING order.
You can sort by column values in pandas DataFrame using sort_values() method. To specify the order, you have to use ascending boolean property; False for descending and True for ascending. By default, it is set to True.
In order to sort the data frame in pandas, function sort_values() is used. Pandas sort_values() can sort the data frame in Ascending or Descending order.
To sort the rows of a DataFrame by a column, use pandas. DataFrame. sort_values() method with the argument by=column_name . The sort_values() method does not modify the original DataFrame, but returns the sorted DataFrame.
You could add drop=FALSE
and it will work with most of the cases. The default option for [
is drop=TRUE
df1[order(df1$col1),, drop=FALSE]
In the help page for ?`[`, the default arguments can be found in the 'Usage'
x[i, j, ... , drop = TRUE]
and the description for drop
as
drop: For matrices and arrays. If ‘TRUE’ the result is coerced to the lowest possible dimension (see the examples). This only works for extracting elements, not for the replacement. See ‘drop’ for further details.
With the data.table package, you don't need the drop = FALSE
:
library(data.table)
setorder(setDT(df1), col1)
which gives:
> df1 col1 1: 0.08243274 2: 0.08955160 3: 0.21051232 4: 0.23750327 5: 0.28552695 6: 0.33195997 7: 0.38523617 8: 0.73295527 9: 0.78839789 10: 0.85213354
Or directly on a dataframe without converting to a data.table
:
library(data.table)
setorder(df1, col1)
which gives:
> df1 col1 7 0.08243274 1 0.08955160 2 0.21051232 9 0.23750327 8 0.28552695 6 0.33195997 10 0.38523617 3 0.73295527 5 0.78839789 4 0.85213354
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With