引用 R 中函数内的数据框
referring to a data frame inside a function in R
我有一个非常大的数据框,格式如下
uniqueID
year
header_1
header_2
c
d
etc.
0001
1990
x
TRUE
0002
1990
y
FALSE
other data
0003
1995
x
FALSE
我可以像这样过滤、总结和重新排列它:
new_df <- filter(df, year %in% c(1990))
count_new_df <- group_by(new_df, header_1, header_2) %>%
summarise(count = n())
count_wide <- count_new_df %>% pivot_wider(names_from = header_1, values_from = count)
如果我运行这是明确的代码,它就可以完美地工作。但是,如果我尝试编写一个函数,其中 d = 起始 df,y = 我想查看的数据年份,并且我为列 headers 插入变量 a、b,它会中断
slice <- function (d,y,a,b) {
t <- filter(d, year %in% c(y))
c <- group_by(t, a, b) %>%
summarise(count = n())
c2 <- c %>% pivot_wider(names_from = a, values_from = count)
}
错误信息:
必须按在“.data”中找到的变量分组,找不到列 'a',找不到列 'b'。
如果我改为调用 d$a 和 d$b,我会得到 object 'a' not found。我也试过 group_by(t, t$a, t$b) 也没有用。我错过了什么?必须有某种方法来调用在函数内部创建的 df 的列。
TIA
您可以使用 {{}}
来引用函数内的列:
library(tidyverse)
new_slice <- function (d,y,a,b) {
t <- filter(d, year %in% y)
c <- group_by(t, {{a}}, {{b}}) %>% summarise(count = n())
#Can also use count
#c <- count(t, {{a}}, {{b}}, name = 'count')
c2 <- c %>% pivot_wider(names_from = {{a}}, values_from = count)
c2
}
new_slice(d, 1990, header_1, header_2)
我有一个非常大的数据框,格式如下
uniqueID | year | header_1 | header_2 | c | d | etc. |
---|---|---|---|---|---|---|
0001 | 1990 | x | TRUE | |||
0002 | 1990 | y | FALSE | other data | ||
0003 | 1995 | x | FALSE |
我可以像这样过滤、总结和重新排列它:
new_df <- filter(df, year %in% c(1990))
count_new_df <- group_by(new_df, header_1, header_2) %>%
summarise(count = n())
count_wide <- count_new_df %>% pivot_wider(names_from = header_1, values_from = count)
如果我运行这是明确的代码,它就可以完美地工作。但是,如果我尝试编写一个函数,其中 d = 起始 df,y = 我想查看的数据年份,并且我为列 headers 插入变量 a、b,它会中断
slice <- function (d,y,a,b) {
t <- filter(d, year %in% c(y))
c <- group_by(t, a, b) %>%
summarise(count = n())
c2 <- c %>% pivot_wider(names_from = a, values_from = count)
}
错误信息: 必须按在“.data”中找到的变量分组,找不到列 'a',找不到列 'b'。
如果我改为调用 d$a 和 d$b,我会得到 object 'a' not found。我也试过 group_by(t, t$a, t$b) 也没有用。我错过了什么?必须有某种方法来调用在函数内部创建的 df 的列。
TIA
您可以使用 {{}}
来引用函数内的列:
library(tidyverse)
new_slice <- function (d,y,a,b) {
t <- filter(d, year %in% y)
c <- group_by(t, {{a}}, {{b}}) %>% summarise(count = n())
#Can also use count
#c <- count(t, {{a}}, {{b}}, name = 'count')
c2 <- c %>% pivot_wider(names_from = {{a}}, values_from = count)
c2
}
new_slice(d, 1990, header_1, header_2)