gpt4 book ai didi

r - 在 dplyr 链中按升序排列列名称

转载 作者:行者123 更新时间:2023-12-02 01:20:56 26 4
gpt4 key购买 nike

我有这个 data.frame:

df <- structure(list(att_number = structure(1:3, .Label = c("0", "1", 
"2"), class = "factor"), `1` = structure(c(2L, 3L, 1L), .Label = c("1026891",
"412419", "424869"), class = "factor"), `10` = structure(c(2L,
1L, 3L), .Label = c("235067", "546686", "92324"), class = "factor"),
`2` = structure(c(3L, 1L, 2L), .Label = c("12729", "7569",
"9149"), class = "factor")), .Names = c("att_number", "1",
"10", "2"), row.names = c(NA, -3L), class = "data.frame")

看起来像这样以数字作为列名。
att_number  1         10        2
0 412419 546686 9149
1 424869 235067 12729
2 1026891 92324 7569

在 dplyr 链中,我想按升序对列进行排序,如下所示:
att_number  1       2      10
0 412419 9149 546686
1 424869 12729 235067
2 1026891 7569 7569

我试过使用 select_ ,但它不想按计划工作。关于我如何做到这一点的任何想法?这是我微弱的尝试:
names_order <- names(df)[-1] %>%
as.numeric %>%
.[order(.)] %>%
as.character %>%
c('att_number', .)

df %>%
select_(.dots = names_order)

Error: Position must be between 0 and n

最佳答案

更新 :

对于较新版本的 dplyr (>= 0.7.0):

library(tidyverse)

sort_names <- function(data) {
name <- names(data)
chars <- keep(name, grepl, pattern = "[^0-9]") %>% sort()
nums <- discard(name, grepl, pattern = "[^0-9]") %>%
as.numeric() %>%
sort() %>%
sprintf("%s", .)

select(data, !!!c(chars, nums))
}

sort_names(df)

原装 :

您需要在数字列名称周围打勾,以阻止 select 尝试将它们解释为列位置:

library(tidyverse)

sort_names <- function(data) {
name <- names(data)
chars <- keep(name, grepl, pattern = "[^0-9]") %>% sort()
nums <- discard(name, grepl, pattern = "[^0-9]") %>%
as.numeric() %>%
sort() %>%
sprintf("`%s`", .)

select_(data, .dots = c(chars, nums))
}

sort_names(df)

关于r - 在 dplyr 链中按升序排列列名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40293509/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com