英文:
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<-function(df, col){
df%>%
rename_with(~ paste0(.,".suffix"))%>%
arrange(paste0(col,".suffix"))}
fn(df=iris, col="Sepal.Width")
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 <- 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
答案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 'suffix'
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论