0

I am trying to sort a dataframe in ascending order which I'm confused at.

Example:

Label | Data
B3        2
B1        3
B2        4

What I'm trying to achieve is:

Label | Data
B1        3
B2        4
B3        2

I am using R without any Library.

Vinícius Félix
  • 8,448
  • 6
  • 16
  • 32
Calvin Chow
  • 31
  • 1
  • 4
  • `dplyr::arrange(mydf, Label)` – Phil Sep 23 '21 at 14:03
  • 1
    Does this answer your question? [Order a "mixed" vector (numbers with letters)](https://stackoverflow.com/questions/20396582/order-a-mixed-vector-numbers-with-letters) – user438383 Sep 23 '21 at 14:14

2 Answers2

0

In base R, you can use order:

Label <- c("B3","B1","B2")
Data <- c(2,3,4)
df <- data.frame(Label,Data)
> df
  Label Data
1    B3    2
2    B1    3
3    B2    4
df = df[order(Label),]
> df
  Label Data
2    B1    3
3    B2    4
1    B3    2
Mata
  • 538
  • 3
  • 17
0

In case you want to use tidyverse later, with dplyr you can use arrange:

Label <- c("B3","B1","B2")
Data <- c(2,3,4)
df <- data.frame(Label,Data)

library(dplyr)

df %>% 
  arrange(Label)

  Label Data
1    B1    3
2    B2    4
3    B3    2
Vinícius Félix
  • 8,448
  • 6
  • 16
  • 32