如何在 R 中按数字对列进行排序?

How to order columns by numbers in R?

我有 data.frame 示例中显示的列名。 V1.1 = 测试系列 1,第 1 天,K1-K3 是三个独特的样本。我想按天对我的专栏进行排序,以便顺序为 V1.1、V1.2、V1.14、V1.21。我认为这不是什么大问题,但我在论坛中找不到我的问题的正确答案。

  A tibble: 6 x 168
      genus V1.1.K1 V1.1.K2 V1.1.K3 V1.14.K16 V1.14.K17 V1.14.K18 V1.2.K4 V1.2.K5 V1.2.K6 V1.21.K19 V1.21.K20 V1.21.K21
      <chr>   <dbl>   <dbl>   <dbl>     <dbl>     <dbl>     <dbl>   <dbl>   <dbl>   <dbl>     <dbl>     <dbl>     <dbl>
    1 A17        58       7      78         0         0         0       0       0       0         0         0         0
    2 Acan~       0       0       0         0         0         0       0       0       0         0         0         0
    3 Acet~       8       0       5       155       256       341       0      28      26       188       142        83
    4 Achr~       0       0       0         0         0         0       0       0       0         0         0         0
    5 Acid~       0       0       7         0         0         0       0      83       6        43        13         0
    6 Acid~       0       0       0         0         0         0       0       0       0         0         0         0

首先,我尝试用

解决问题
select(1:3, 7:9, 4:5, 11:13)

但是我正在使用的其他 data.frames 无法重现,因为列的数量或日期不同。

或者是否有一个参数可以添加到我的数据导入中,这是由

完成的
read_tsv()

? 你会帮我很多:) 凯瑟琳

尝试

New_data_frame <- My_data_frame[, order(c(names(My_data_frame)))]

我认为它应该可以工作,因为它会按字母顺序对列进行排序

既然你想让它们按数字排序,忽略字母(或字典排序),那么这里有一个方法。由于您使用的是 select,我推断您有 %>% 和其他。

library(magrittr) # %>%, not needed if you have dplyr loaded
library(stringr)  # str_extract_all
ord <- str_extract_all(names(dat), "\d+") %>%
  lapply(., `length<-`, max(lengths(.))) %>%
  lapply(., as.integer) %>%
  do.call(rbind.data.frame, .) %>%
  do.call(function(...) order(..., na.last = FALSE), .)

ord
#  [1]  1  2  3  4  8  9 10  5  6  7 11 12 13
dat[,ord]      # or select(dat, all_of(ord))
#   genus V1.1.K1 V1.1.K2 V1.1.K3 V1.2.K4 V1.2.K5 V1.2.K6 V1.14.K16 V1.14.K17 V1.14.K18 V1.21.K19 V1.21.K20 V1.21.K21
# 1   A17      58       7      78       0       0       0         0         0         0         0         0         0
# 2 Acan~       0       0       0       0       0       0         0         0         0         0         0         0
# 3 Acet~       8       0       5       0      28      26       155       256       341       188       142        83
# 4 Achr~       0       0       0       0       0       0         0         0         0         0         0         0
# 5 Acid~       0       0       7       0      83       6         0         0         0        43        13         0
# 6 Acid~       0       0       0       0       0       0         0         0         0         0         0         0

na.last=FALSE 强制(在本例中)genus 成为第一个。如果您有各种带数字和不带数字的名称,您可能会得到不一致的结果,在这种情况下,您可能需要对您使用的列名称进行子集处理。


数据

dat <- structure(list(genus = c("A17", "Acan~", "Acet~", "Achr~", "Acid~", "Acid~"), V1.1.K1 = c(58L, 0L, 8L, 0L, 0L, 0L), V1.1.K2 = c(7L, 0L, 0L, 0L, 0L, 0L), V1.1.K3 = c(78L, 0L, 5L, 0L, 7L, 0L), V1.14.K16 = c(0L, 0L, 155L, 0L, 0L, 0L), V1.14.K17 = c(0L, 0L, 256L, 0L, 0L, 0L), V1.14.K18 = c(0L, 0L, 341L, 0L, 0L, 0L), V1.2.K4 = c(0L, 0L, 0L, 0L, 0L, 0L), V1.2.K5 = c(0L, 0L, 28L, 0L, 83L, 0L), V1.2.K6 = c(0L, 0L, 26L, 0L, 6L, 0L), V1.21.K19 = c(0L, 0L, 188L, 0L, 43L, 0L), V1.21.K20 = c(0L, 0L, 142L, 0L, 13L, 0L), V1.21.K21 = c(0L, 0L, 83L, 0L, 0L, 0L)), class = "data.frame", row.names = c("1", "2", "3", "4", "5", "6"))