我正在摆弄内置数据集economics
在 R 中,我试图将数据帧列作为参数传递到使用管道的函数中(dplyr https://genomicsclass.github.io/book/pages/dplyr_tutorial.html, %>%
)。但我遇到了一些看似奇怪的问题。不知何故,我无法成功地将列名作为参数传递给函数top_n() https://dplyr.tidyverse.org/reference/top_n.html在我的自定义函数中。以下是我对人口最多的 5 个国家进行子集划分的方法without自定义函数:
Code 1:
library(dplyr)
df_econ <- economics
df_top_5 <- df_econ %>% top_n(5, pop)
df_top_5
输出1:
2014-12-01 12122.0 320201 5.0 12.6 8688
2015-01-01 12080.8 320367 5.5 13.4 8979
2015-02-01 12095.9 320534 5.7 13.1 8705
2015-03-01 12161.5 320707 5.2 12.2 8575
2015-04-01 12158.9 320887 5.6 11.7 8549
包装到自定义函数中,它可能如下所示:
Code 2:
library(dplyr)
# data
data(economics)
df_econ <- economics
# custom function
fxtop <- function(df, number, column){
tops <- df %>% top_n(number, column)
return(tops)
}
# build a df using custom function
df_top_5 <- fxtop(df=df_econ, number=5, column='pop')
df_top_5
输出2:
1967-07-01 507.4 198712 12.5 4.5 2944
1967-08-01 510.5 198911 12.5 4.7 2945
1967-09-01 516.3 199113 11.7 4.6 2958
1967-10-01 512.9 199311 12.5 4.9 3143
1967-11-01 518.1 199498 12.5 4.7 3066
1967-12-01 525.8 199657 12.1 4.8 3018
1968-01-01 531.5 199808 11.7 5.1 2878
1968-02-01 534.2 199920 12.2 4.5 3001
1968-03-01 544.9 200056 11.6 4.1 2877
1968-04-01 544.6 200208 12.2 4.6 2709
此输出有 10 行,而不是预期的 5 行。我怀疑这个论点number=5
被简单地忽略,实际使用的数字默认为10
。数据似乎没有排序'pop'
either.
到目前为止我尝试过的:
尝试1:硬编码pop
and number
在自定义函数中:
library(dplyr)
# data
data(economics)
df_econ <- economics
# custom function
fxtop <- function(df, number, column){
tops <- df %>% top_n(5, pop)
return(tops)
}
# build a df using custom function
df_top_5 <- fxtop(df=df_econ, number=5, column='pop')
df_top_5
尝试 1:输出:
2014-12-01 12122.0 320201 5.0 12.6 8688
2015-01-01 12080.8 320367 5.5 13.4 8979
2015-02-01 12095.9 320534 5.7 13.1 8705
2015-03-01 12161.5 320707 5.2 12.2 8575
2015-04-01 12158.9 320887 5.6 11.7 8549
尝试1: Comment
这就是想要的输出!
让我们看看当我通过函数传递变量时会发生什么
尝试2:将变量作为对象而不是字符串传递:
library(dplyr)
# data
data(economics)
df_econ <- economics
# custom function
fxtop <- function(df, number, column){
tops <- df %>% top_n(5, column)
return(tops)
}
# build a df using custom function
df_top_5 <- fxtop(df=df_econ, number=5, column='pop')
df_top_5
尝试 2:输出:
现在的输出与第一个示例中的相同。这两个变量似乎都被忽略了。
那么,有什么建议吗?