I'm having trouble with sorting my dataframe in R.
It is a large dataframe in which there is 1 variable (the mean absolute deviation), referred to as mads as the columnname
There are more than 300.000 rows in which every row has a seperate name.
I want to order the mean absolute deviation is descending order, but also contain the corresponding rowname.
I only succeed in receiving the descending order but losing the corresponding row name.
library(tidyverse)
#construct example data
set.seed(42)
#inventing mads
example_data <- data.frame(mads = runif(300000,0,1))
#inventing rownames
rn_to_use <- head(combn(x = c(LETTERS,letters,0:9),m = 4,simplify = FALSE),n=300000) %>%
map_chr(~paste0(.,collapse=""))
rownames(example_data) <- rn_to_use
#solution
arrange(example_data,desc(mads))
Hope the above helps.
As you can see preparing an example took me more effort than solving for what you want...
in the future you could provide representative data. the R package datapasta has wonderful functions for that.
for example the first 20records from the data I used could have been generated and shared here like so :
datapasta::df_paste(head(example_data,20))
this provides the text:
data.frame(
row.names = c("ABCD","ABCE","ABCF","ABCG","ABCH",
"ABCI","ABCJ","ABCK","ABCL","ABCM","ABCN","ABCO","ABCP",
"ABCQ","ABCR","ABCS","ABCT","ABCU","ABCV","ABCW"),
mads = c(0.914806043496355,0.937075413297862,
0.286139534786344,0.830447626067325,0.641745518893003,
0.519095949130133,0.736588314641267,0.13466659723781,0.656992290401831,
0.705064784036949,0.45774177624844,0.719112251652405,
0.934672247152776,0.255428824340925,0.462292822543532,
0.940014522755519,0.978226428385824,0.117487361654639,0.474997081561014,
0.560332746244967)
Which could form the data part of reprex
library(tidyverse)
example_data <- data.frame(
row.names = c("ABCD","ABCE","ABCF","ABCG","ABCH",
"ABCI","ABCJ","ABCK","ABCL","ABCM","ABCN","ABCO","ABCP",
"ABCQ","ABCR","ABCS","ABCT","ABCU","ABCV","ABCW"),
mads = c(0.914806043496355,0.937075413297862,
0.286139534786344,0.830447626067325,0.641745518893003,
0.519095949130133,0.736588314641267,0.13466659723781,0.656992290401831,
0.705064784036949,0.45774177624844,0.719112251652405,
0.934672247152776,0.255428824340925,0.462292822543532,
0.940014522755519,0.978226428385824,0.117487361654639,0.474997081561014,
0.560332746244967)
#solution
arrange(example_data,desc(mads))
This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.
If you have a query related to it or one of the replies, start a new topic and refer back with a link.