How can I sort a data.frame with only one column, without losing rownames?
You were close with your first attempt, just forgot about using drop = FALSE
:
> d[order(-d$data), , drop = FALSE]
data
yak 4
baz 3
bar 2
foo 1
How to sort each row of a data frame WITHOUT losing the column names
Store the names and apply them:
nm = names(df)
sorted_df <- as.data.frame(t(apply(df, 1, sort)))
names(sorted_df) = nm
You could compress this down to a single line if you prefer:
sorted_df = setNames(as.data.frame(t(apply(df, 1, sort))), names(df))
Is there an elegant way to reorder data frame by rownames in R
Works just fine with arrange()
:
library(dplyr)
d <- read.table(textConnection("
value
gene8 0.140533602
gene6 21.129493396
gene7 0.170091711
gene3 28.101415822
gene1 0.005706749
gene2 3.157911375"))
d %>%
arrange(row.names(.))
#> value
#> gene1 0.005706749
#> gene2 3.157911375
#> gene3 28.101415822
#> gene6 21.129493396
#> gene7 0.170091711
#> gene8 0.140533602
Sorting a dataframe by a column name passed to a function
You can write the function sort.by.column
using order
as:
sort.by.column <- function(df, column.name) {
df[order(df[,column.name]),]
}
#Lets test the function
sort.by.column(df1, "Value")
# Instrument Value
# 2 B 2
# 1 A 3
sort.by.column(df2, "Value")
# Device Value
# 2 D 4
# 1 C 5
sort.by.column(df1, "Instrument")
# Instrument Value
# 1 A 3
# 2 B 2
sort.by.column(df2, "Device")
# Device Value
# 1 C 5
# 2 D 4
Data:
df1 <- data.frame(Instrument=c("A","B"),
Value=c(3,2))
df2 <- data.frame(Device=c("C","D"),
Value=c(5,4))
Explanation
It works like this. With the initialization
df <- data.frame(Instrument=c("B","A"), Value=c(3,2))
col.name <- "Instrument"
the expression
df[,col.name]
returns a 1-column subset of the data frame. The expression
order(df[,col.name])
sorts that subset, returning the indices of the rows. Finally
df[order(df[,col.name]),]
returns a row-subset of the dataframe, with the row indices in the order computed.
Use order on a single column data frame
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.
How to order the default column from 1 to n?
If you want to re-order the rows by row names, you can extract them with rownames
, convert it to numeric and then order
df[order(as.integer(rownames(df))), ]
Or if you just want to rename them from 1:N
instead of current names
rownames(df) <- NULL
How can I use the row.names attribute to order the rows of my dataframe in R?
This worked for me:
new_df <- df[ order(row.names(df)), ]
Related Topics
Remove All Variables Except Functions
Simple Manual Rmarkdown Tables That Look Good in HTML, PDF and Docx
How to Plot the Results of a Mixed Model
Controlling the 'Alpha' Level in a Ggplot2 Legend
Add Author Affiliation in R Markdown Beamer Presentation
How to Build a Dendrogram from a Directory Tree
How to Use the Box-Cox Power Transformation in R
Lapply Function /Loops on List of Lists R
Predicting Lda Topics for New Data
Kruskal-Wallis Test with Details on Pairwise Comparisons
R Cmd Check Note: Found No Calls To: 'R_Registerroutines', 'R_Usedynamicsymbols'
Handling Special Characters E.G. Accents in R
How to Get the Most Frequent Level of a Categorical Variable in R