使用dplyr mutate自动生成新变量名
我想在使用dplyr时动态创建变量名;不过,我也可以使用非dplyr解决方案 例如:使用dplyr mutate自动生成新变量名,r,dplyr,R,Dplyr,我想在使用dplyr时动态创建变量名;不过,我也可以使用非dplyr解决方案 例如: data(iris) library(dplyr) iris <- iris %>% group_by(Species) %>% mutate( lag_Sepal.Length = lag(Sepal.Length), lag_Sepal.Width = lag(Sepal.Width), lag_Petal.Length = lag(Petal.Len
data(iris)
library(dplyr)
iris <- iris %>%
group_by(Species) %>%
mutate(
lag_Sepal.Length = lag(Sepal.Length),
lag_Sepal.Width = lag(Sepal.Width),
lag_Petal.Length = lag(Petal.Length)
) %>%
ungroup
head(iris)
Sepal.Length Sepal.Width Petal.Length Petal.Width Species lag_Sepal.Length lag_Sepal.Width
(dbl) (dbl) (dbl) (dbl) (fctr) (dbl) (dbl)
1 5.1 3.5 1.4 0.2 setosa NA NA
2 4.9 3.0 1.4 0.2 setosa 5.1 3.5
3 4.7 3.2 1.3 0.2 setosa 4.9 3.0
4 4.6 3.1 1.5 0.2 setosa 4.7 3.2
5 5.0 3.6 1.4 0.2 setosa 4.6 3.1
6 5.4 3.9 1.7 0.4 setosa 5.0 3.6
Variables not shown: lag_Petal.Length (dbl)
数据(iris)
图书馆(dplyr)
虹膜%
组别(种类)%>%
变异(
萼片长度=萼片长度,
萼片宽度=萼片宽度,
滞后花瓣长度=滞后(花瓣长度)
) %>%
解组
头部(虹膜)
萼片。长萼片。宽花瓣。长花瓣。宽种滞后萼片。长滞后萼片。宽
(dbl)(dbl)(dbl)(dbl)(fctr)(dbl)(dbl)(dbl)
1 5.1 3.5 1.4 0.2刚毛
2 4.9 3.0 1.4 0.2 setosa 5.1 3.5
3 4.7 3.2 1.3 0.2 setosa 4.9 3.0
4.6 3.1 1.5 0.2 setosa 4.7 3.2
5.0 3.6 1.4 0.2 setosa 4.6 3.1
6 5.4 3.9 1.7 0.4 setosa 5.0 3.6
未显示的变量:lag_Petal.Length(dbl)
但是,我不想这样做三次,而是想创建100个“lag”变量,它们的名称是:lag_原始变量名。我正试图弄清楚如何在不输入100次新变量名的情况下实现这一点,但我的答案很短
我已经研究了其他地方的例子。它们很相似,但我不太能够拼凑出我需要的具体解决方案。感谢您的帮助
编辑感谢@BenFasoli的启发。我接受了他的答案,并对其进行了一些调整,以得到我需要的解决方案。 我还使用和。变量名中的“lag”是尾随而不是前导,但我可以接受 我的最终代码发布在这里,以防对其他人有所帮助:
lagged <- iris %>%
group_by(Species) %>%
mutate_at(
vars(Sepal.Length:Petal.Length),
funs("lag" = lag)) %>%
ungroup
# A tibble: 6 x 8
Sepal.Length Sepal.Width Petal.Length Petal.Width Species Sepal.Length_lag Sepal.Width_lag
<dbl> <dbl> <dbl> <dbl> <fctr> <dbl> <dbl>
1 5.1 3.5 1.4 0.2 setosa NA NA
2 4.9 3.0 1.4 0.2 setosa 5.1 3.5
3 4.7 3.2 1.3 0.2 setosa 4.9 3.0
4 4.6 3.1 1.5 0.2 setosa 4.7 3.2
5 5.0 3.6 1.4 0.2 setosa 4.6 3.1
6 5.4 3.9 1.7 0.4 setosa 5.0 3.6
# ... with 1 more variables: Petal.Length_lag <dbl>
滞后%
组别(种类)%>%
突变(
变异(萼片长度:花瓣长度),
funs(“滞后”=滞后))%>%
解组
#一个tibble:6x8
萼片。长萼片。宽花瓣。长花瓣。宽种萼片。长萼片。宽萼片
1 5.1 3.5 1.4 0.2刚毛
2 4.9 3.0 1.4 0.2 setosa 5.1 3.5
3 4.7 3.2 1.3 0.2 setosa 4.9 3.0
4.6 3.1 1.5 0.2 setosa 4.7 3.2
5.0 3.6 1.4 0.2 setosa 4.6 3.1
6 5.4 3.9 1.7 0.4 setosa 5.0 3.6
# ... 还有1个变量:Petal.Length\u lag
您可以使用mutate\u all
(或mutate\u at
针对特定列),然后在列名前添加lag
data(iris)
library(dplyr)
lag_iris <- iris %>%
group_by(Species) %>%
mutate_all(funs(lag(.))) %>%
ungroup
colnames(lag_iris) <- paste0('lag_', colnames(lag_iris))
head(lag_iris)
lag_Sepal.Length lag_Sepal.Width lag_Petal.Length lag_Petal.Width lag_Species
<dbl> <dbl> <dbl> <dbl> <fctr>
1 NA NA NA NA setosa
2 5.1 3.5 1.4 0.2 setosa
3 4.9 3.0 1.4 0.2 setosa
4 4.7 3.2 1.3 0.2 setosa
5 4.6 3.1 1.5 0.2 setosa
6 5.0 3.6 1.4 0.2 setosa
数据(iris)
图书馆(dplyr)
滞后率%
组别(种类)%>%
突变所有(funs(滞后(%))%%>%
解组
colnames(lag_iris)这里是一个data.table方法。在本例中,我选择了带有数字的列。您要做的是选择列名并提前创建新列名。然后,将shift()
应用于所选的每一列,其工作原理类似于dplyr包中的lag()
和lead()
library(data.table)
# Crate a df for this demo.
mydf <- iris
# Choose columns that you want to apply lag() and create new colnames.
cols = names(iris)[sapply(iris, is.numeric)]
anscols = paste("lag_", cols, sep = "")
# Apply shift() to each of the chosen columns.
setDT(mydf)[, (anscols) := shift(.SD, 1, type = "lag"),
.SDcols = cols]
Sepal.Length Sepal.Width Petal.Length Petal.Width Species lag_Sepal.Length lag_Sepal.Width
1: 5.1 3.5 1.4 0.2 setosa NA NA
2: 4.9 3.0 1.4 0.2 setosa 5.1 3.5
3: 4.7 3.2 1.3 0.2 setosa 4.9 3.0
4: 4.6 3.1 1.5 0.2 setosa 4.7 3.2
5: 5.0 3.6 1.4 0.2 setosa 4.6 3.1
---
146: 6.7 3.0 5.2 2.3 virginica 6.7 3.3
147: 6.3 2.5 5.0 1.9 virginica 6.7 3.0
148: 6.5 3.0 5.2 2.0 virginica 6.3 2.5
149: 6.2 3.4 5.4 2.3 virginica 6.5 3.0
150: 5.9 3.0 5.1 1.8 virginica 6.2 3.4
lag_Petal.Length lag_Petal.Width
1: NA NA
2: 1.4 0.2
3: 1.4 0.2
4: 1.3 0.2
5: 1.5 0.2
---
146: 5.7 2.5
147: 5.2 2.3
148: 5.0 1.9
149: 5.2 2.0
150: 5.4 2.3
库(data.table)
#为这个演示用板条箱装一个df。
mydf既然您对非dplyr也很满意,请尝试以下方法:
lagger <- function(x, n) c(rep(NA,n), head(x,-n) )
iris[paste0("lag_", names(iris) )] <- lapply(iris, lagger, n=1)
head(iris,2)[-(1:5)]
# lag_Sepal.Length lag_Sepal.Width lag_Petal.Length lag_Petal.Width lag_Species
#1 NA NA NA NA NA
#2 5.1 3.5 1.4 0.2 1
lagger谢谢你的灵感。我接受了你的答案,并对它进行了一些调整,以得到我需要的解决方案。我还使用了[这篇RStudio博客]()和[这篇SO帖子]()。我最后的代码发布在这里,以防对其他人有所帮助:lagged%groupby(Species)%%>%mutate_at(vars(萼片长度:花瓣长度),funs(“lag”=lag))%%>%ungroup
再次感谢!