I have a dataset that looks something like the below. Is there a line of code in R to arrange the different species from most abundant (occurs the most in the column) to least abundant? Thank you!
"Species A"
"Species A"
"Species B"
"Species C"
"Species A"
"Species C"
"Species B"
"Species A"
CodePudding user response:
There is the function fct_infreq
from the forcats package to order elements by their abundance:
library(tidyverse)
data <- tibble(
species = c(
"Species A",
"Species A",
"Species B",
"Species C",
"Species A",
"Species C",
"Species B",
"Species A")
)
data %>%
mutate(species = species %>% fct_infreq()) %>%
arrange(species)
#> # A tibble: 8 × 1
#> species
#> <fct>
#> 1 Species A
#> 2 Species A
#> 3 Species A
#> 4 Species A
#> 5 Species B
#> 6 Species B
#> 7 Species C
#> 8 Species C
Created on 2022-05-11 by the reprex package (v2.0.0)
CodePudding user response:
You can try to use table
, sort this, use match
to the names
and order
.
df[order(match(df$name, names(sort(table(df$name), decreasing=TRUE)))),]
## A tibble: 8 × 1
# name
# <chr>
#1 Species A
#2 Species A
#3 Species A
#4 Species A
#5 Species B
#6 Species B
#7 Species C
#8 Species C
CodePudding user response:
You can also reorder
the factor by number of occurrences, and then sort
the factor:
data$species <- reorder(data$species, data$species, FUN = length) |>
sort(decreasing = T)
output
data
species
1 Species A
2 Species A
3 Species A
4 Species A
5 Species C
6 Species C
7 Species B
8 Species B