如何在dplyr中更改循环 [英] How to mutate for loop in dplyr
问题描述
我想为数据框中的一列创建多个滞后变量,以获取一系列值.我的代码可以成功完成我想做的事情,但不能满足我的需要(数百次迭代)
I want to create multiple lag variables for a column in a data frame for a range of values. I have code that successfully does what I want but is not scalable for what I need (hundreds of iterations)
下面有我的代码,可以成功完成我想做的事,但不能满足我的需要(数百次迭代)
I have code below that successfully does what I want but is not scalable for what I need (hundreds of iterations)
Lake_Lag <- Lake_Champlain_long.term_monitoring_1992_2016 %>%
group_by(StationID,Test) %>%
arrange(StationID,Test,VisitDate) %>%
mutate(lag.Result1 = dplyr::lag(Result, n = 1, default = NA))%>%
mutate(lag.Result5 = dplyr::lag(Result, n = 5, default = NA))%>%
mutate(lag.Result10 = dplyr::lag(Result, n = 10, default = NA))%>%
mutate(lag.Result15 = dplyr::lag(Result, n = 15, default = NA))%>%
mutate(lag.Result20 = dplyr::lag(Result, n = 20, default = NA))
我希望能够使用列表c(1,5,10,15,20)或范围1:150为数据框创建滞后变量.
I would like to be able to use a list c(1,5,10,15,20) or a range 1:150 to create lagging variables for my data frame.
推荐答案
这是一种利用来自 rlang 的
dplyr
中包含的整洁的评估助手"的方法.code>包.
Here's an approach that makes use of some 'tidy eval helpers' included in dplyr
that come from the rlang
package.
基本思想是在 mutate()
中创建一个新列,其名称基于for循环提供的字符串.
The basic idea is to create a new column in mutate()
whose name is based on a string supplied by a for-loop.
library(dplyr)
grouped_data <- Lake_Champlain_long.term_monitoring_1992_2016 %>%
group_by(StationID,Test) %>%
arrange(StationID,Test,VisitDate)
for (lag_size in c(1, 5, 10, 15, 20)) {
new_col_name <- paste0("lag_result_", lag_size)
grouped_data <- grouped_data %>%
mutate(!!sym(new_col_name) := lag(Result, n = lag_size, default = NA))
}
sym(new_col_name):=
是在使用<
这篇关于如何在dplyr中更改循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!