使用R语言对数据进行排序,使用已经添加了后缀的命名列。

huangapple go评论80阅读模式
英文:

Sorting data in a function using a named column that has had a suffix added to it using R

问题

我有一个函数,它接受一个数据框和一个列

我想按列名排序

在数据流中,所有字段都会添加后缀

如何使用arrange函数并考虑后缀?

library(tidyverse)

fn <- function(df, col) {
  df %>%
    rename_with(~ paste0(., ".suffix")) %>%
    arrange(!!sym(paste0(col, ".suffix")))
}

fn(df = iris, col = "Sepal.Width")

可以有人修改上面的代码,以便它将按Sepal.Width.suffix列进行排序吗?

英文:

I have a function that takes a dataframe and a column

I want to sort by the column name

Along the dataflow, a suffix is added to all the fields within the dataframe

how can I use the arrange function and also take account for the suffix?

library(tidyverse)

fn&lt;-function(df, col){
df%&gt;%
rename_with(~ paste0(.,&quot;.suffix&quot;))%&gt;%
arrange(paste0(col,&quot;.suffix&quot;))}

fn(df=iris, col=&quot;Sepal.Width&quot;)

Can anyone amend the above code so that it will sort by the Sepal.Width.suffix column?

答案1

得分: 1

一个选项是在arrange函数中使用.data代词:

注意:有关.data代词的更多信息,请参阅子集化.data代词

library(dplyr, warn=FALSE)

fn <- function(df, col) {
  df %>%
    rename_with(~ paste0(., ".suffix")) %>%
    arrange(.data[[paste0(col, ".suffix")]])
}

df_sorted <- fn(df = iris, col = "Sepal.Width")

head(df_sorted)
#>   Sepal.Length.suffix Sepal.Width.suffix Petal.Length.suffix Petal.Width.suffix
#> 1                 5.0                2.0                 3.5                1.0
#> 2                 6.0                2.2                 4.0                1.0
#> 3                 6.2                2.2                 4.5                1.5
#> 4                 6.0                2.2                 5.0                1.5
#> 5                 4.5                2.3                 1.3                0.3
#> 6                 5.5                2.3                 4.0                1.3
#>   Species.suffix
#> 1     versicolor
#> 2     versicolor
#> 3     versicolor
#> 4      virginica
#> 5         setosa
#> 6     versicolor
英文:

One option would be to use the .data pronoun in arrange:

Note: For more on the .data pronoun see e.g. Subsetting the .data pronoun.

library(dplyr, warn=FALSE)

fn &lt;- function(df, col) {
  df %&gt;%
    rename_with(~ paste0(., &quot;.suffix&quot;)) %&gt;%
    arrange(.data[[paste0(col, &quot;.suffix&quot;)]])
}

df_sorted &lt;- fn(df = iris, col = &quot;Sepal.Width&quot;)

head(df_sorted)
#&gt;   Sepal.Length.suffix Sepal.Width.suffix Petal.Length.suffix Petal.Width.suffix
#&gt; 1                 5.0                2.0                 3.5                1.0
#&gt; 2                 6.0                2.2                 4.0                1.0
#&gt; 3                 6.2                2.2                 4.5                1.5
#&gt; 4                 6.0                2.2                 5.0                1.5
#&gt; 5                 4.5                2.3                 1.3                0.3
#&gt; 6                 5.5                2.3                 4.0                1.3
#&gt;   Species.suffix
#&gt; 1     versicolor
#&gt; 2     versicolor
#&gt; 3     versicolor
#&gt; 4      virginica
#&gt; 5         setosa
#&gt; 6     versicolor

答案2

得分: -1

根据工作流程中对数据进行“审计”的需要,可以使用base包重新排列列并排序,同时保留“数据来源”行号:

iris2 = iris[, c(2,1,3,4,5)] # 对“后缀”不敏感
head(iris2[ do.call(order, iris2), ], n = 6)
    花萼宽度 花萼长度 花瓣长度 花瓣宽度    物种
61     2.0     5.0     3.5    1.0  山鸢尾
63     2.2     6.0     4.0    1.0  山鸢尾
120    2.2     6.0     5.0    1.5  维吉尼亚
69     2.2     6.2     4.5    1.5  山鸢尾
42     2.3     4.5     1.3    0.3  山鸢尾
94     2.3     5.0     3.3    1.0  山鸢尾

如有需要,可以再次更改列顺序。

英文:

Depending on a need to 'audit' one's data as it churns through the workflow, one might, using base, reorder columns and sort, while preserving the 'where did it came from' row numbers:

iris2 = iris[, c(2,1,3,4,5)] # agnostic to &#39;suffix&#39;
head(iris2[ do.call(order, iris2), ], n = 6)
    Sepal.Width Sepal.Length Petal.Length Petal.Width    Species
61          2.0          5.0          3.5         1.0 versicolor
63          2.2          6.0          4.0         1.0 versicolor
120         2.2          6.0          5.0         1.5  virginica
69          2.2          6.2          4.5         1.5 versicolor
42          2.3          4.5          1.3         0.3     setosa
94          2.3          5.0          3.3         1.0 versicolor

And change column order again if desirable.

huangapple
  • 本文由 发表于 2023年7月31日 21:10:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76803964.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定