如何在管道中检测模式以重新定位列

问题描述 投票:0回答:1

我正在努力将描述性统计列重新定位到靠近原始列的位置

iris2 <- iris %>% dplyr::select(c(2:4)) %>% dplyr::mutate(across(.cols = where(is.numeric), .fns = list(
        n_miss = ~ sum(is.na(.x)),
        mean   = ~ mean(.x, na.rm = TRUE),
        median = ~ median(.x, na.rm = TRUE),
        min    = ~ min(.x, na.rm = TRUE),
        max    = ~ max(.x, na.rm = TRUE)
      )))

如您所见,我在计算描述性统计数据后生成的描述性列位于数据框的末尾,我想订购。

所以我有两个相关问题;我将粘贴一些语法(它们都不起作用):

#First: how to pass the pattern using str_detect or grep to relocate descriptive columns

iris2 <- iris2 %>% dplyr::relocate(str_detect(colnames, "Sepal.Width"), .after =   Sepal.Width)

# Second: would it be possible to do it for all columns, passing the name in a vector 

iris2 <- iris2 %>% dplyr::relocate(str_detect(colnames, c("Sepal.Width", "Petal.Width"), .after = list(Sepal.Width,  Petal.Width)))

每列的预期输出都是这样的(我粘贴 Sepal.Width)

expected_output <- structure(list(Sepal.Width = c(3.5, 3, 3.2, 3.1, 3.6, 3.9), Sepal.Width_mean = c(3.05733333333333, 
3.05733333333333, 3.05733333333333, 3.05733333333333, 3.05733333333333, 
3.05733333333333), Sepal.Width_median = c(3, 3, 3, 3, 3, 3), 
    Sepal.Width_min = c(2, 2, 2, 2, 2, 2), Sepal.Width_max = c(4.4, 
    4.4, 4.4, 4.4, 4.4, 4.4), Petal.Length = c(1.4, 1.4, 1.3, 
    1.5, 1.4, 1.7)), row.names = c(NA, 6L), class = "data.frame")

我没有尝试排列功能,因为我期望在管道中执行这些操作,但如果不可能我会分两步完成

谢谢!

r grep relocation relocate
1个回答
0
投票

一种方法是多字符串

order
,首先按名称本身排序,然后按
_
之后的部分排序。

select(iris2, order(
  colnames(iris2),
  match(sub(".*_", "", colnames(iris2)), c("n_miss", "mean", "median", "min", "max"), nomatch=0))
) |>
  str()
# 'data.frame': 150 obs. of  18 variables:
#  $ Petal.Length       : num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
#  $ Petal.Length_max   : num  6.9 6.9 6.9 6.9 6.9 6.9 6.9 6.9 6.9 6.9 ...
#  $ Petal.Length_mean  : num  3.76 3.76 3.76 3.76 3.76 ...
#  $ Petal.Length_median: num  4.35 4.35 4.35 4.35 4.35 4.35 4.35 4.35 4.35 4.35 ...
#  $ Petal.Length_min   : num  1 1 1 1 1 1 1 1 1 1 ...
#  $ Petal.Length_n_miss: int  0 0 0 0 0 0 0 0 0 0 ...
#  $ Petal.Width        : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
#  $ Petal.Width_max    : num  2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 2.5 ...
#  $ Petal.Width_mean   : num  1.2 1.2 1.2 1.2 1.2 ...
#  $ Petal.Width_median : num  1.3 1.3 1.3 1.3 1.3 1.3 1.3 1.3 1.3 1.3 ...
#  $ Petal.Width_min    : num  0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 0.1 ...
#  $ Petal.Width_n_miss : int  0 0 0 0 0 0 0 0 0 0 ...
#  $ Sepal.Width        : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
#  $ Sepal.Width_max    : num  4.4 4.4 4.4 4.4 4.4 4.4 4.4 4.4 4.4 4.4 ...
#  $ Sepal.Width_mean   : num  3.06 3.06 3.06 3.06 3.06 ...
#  $ Sepal.Width_median : num  3 3 3 3 3 3 3 3 3 3 ...
#  $ Sepal.Width_min    : num  2 2 2 2 2 2 2 2 2 2 ...
#  $ Sepal.Width_n_miss : int  0 0 0 0 0 0 0 0 0 0 ...
© www.soinside.com 2019 - 2024. All rights reserved.