Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use order on a single column data frame

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?

like image 704
screechOwl Avatar asked Jul 15 '15 14:07

screechOwl


People also ask

How do I order a DataFrame in R by one column?

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.

How do you sort DataFrame rows based on one column?

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.

How do you order values in a data frame?

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.

How do I sort a pandas DataFrame by column?

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.


2 Answers

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.

like image 197
akrun Avatar answered Oct 01 '22 19:10

akrun


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
like image 40
Jaap Avatar answered Oct 01 '22 20:10

Jaap