我有這個df:
table
C1 C10 C11 C12 C13 C14 C15 C16 C17 C18 C2 C3 C4 C5 C6 C7 C8 C9
Mest 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Dlk1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0
Meg3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
我想訂購這些列,我試過了:
colnames(table) <- stringr::str_sort(colnames(table),numeric = TRUE)
但它只會按字母順序更改列的名稱,使列保持在相同的位置:
table
C1 C2 C3 C4 C5 C6 C7 C8 C9 C10 C11 C12 C13 C14 C15 C16 C17 C18
Mest 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Dlk1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0
Meg3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
uj5u.com熱心網友回復:
基數 R,
DAT[,order(as.integer(sub("\\D", "", names(DAT))))]
# C1 C2 C3 C4 C5 C6 C7 C8 C9 C10 C11 C12 C13 C14 C15 C16 C17 C18
# Mest 1 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0
# Dlk1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
# Meg3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
uj5u.com熱心網友回復:
這是另一種stringr
解決方案。它使用str_order
,不是str_sort
。
table <- table[stringr::str_order(names(table), numeric = TRUE)]
table
#> C1 C2 C3 C4 C5 C6 C7 C8 C9 C10 C11 C12 C13 C14 C15 C16 C17 C18
#> Mest 1 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0
#> Dlk1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
#> Meg3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
使用reprex v2.0.2創建于 2022-10-27
資料
table <-
structure(list(C1 = c(1L, 0L, 0L), C10 = c(1L, 0L, 0L), C11 = c(1L,
0L, 0L), C12 = c(1L, 0L, 0L), C13 = c(0L, 0L, 0L), C14 = c(0L,
0L, 0L), C15 = c(0L, 0L, 0L), C16 = c(0L, 0L, 0L), C17 = c(0L,
0L, 0L), C18 = c(0L, 0L, 0L), C2 = c(0L, 0L, 0L), C3 = c(0L,
0L, 0L), C4 = c(0L, 1L, 0L), C5 = c(0L, 0L, 0L), C6 = c(0L, 0L,
0L), C7 = c(0L, 0L, 0L), C8 = c(0L, 0L, 0L), C9 = c(0L, 0L, 0L
)), row.names = c("Mest", "Dlk1", "Meg3"), class = "data.frame")
uj5u.com熱心網友回復:
使用dplyr
library(dplyr)
library(stringr)
df <- tibble::tribble(
~V1, ~C1, ~C10, ~C11, ~C12, ~C13, ~C14, ~C15, ~C16, ~C17, ~C18, ~C2, ~C3, ~C4, ~C5, ~C6, ~C7, ~C8, ~C9,
"Mest", 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
"Dlk1", 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L,
"Meg3", 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L
)
select_order <- stringr::str_sort(names(df), numeric = TRUE)
df %>%
select(all_of(select_order))
#> # A tibble: 3 × 19
#> C1 C2 C3 C4 C5 C6 C7 C8 C9 C10 C11 C12 C13
#> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int>
#> 1 1 0 0 0 0 0 0 0 0 1 1 1 0
#> 2 0 0 0 1 0 0 0 0 0 0 0 0 0
#> 3 0 0 0 0 0 0 0 0 0 0 0 0 0
#> # … with 6 more variables: C14 <int>, C15 <int>, C16 <int>, C17 <int>,
#> # C18 <int>, V1 <chr>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/522293.html
標籤:r