2016-07-12 15 views
3

dplyrを使用しているときに変数名を動的に作成したいと思います。しかし、私は非dplyrソリューションでもうまくいくでしょう。dplyr mutateを使用して新しい変数名を自動的に生成

例の場合

:lag_original変数名:

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) 

しかし、代わりにこの三回やって、私は名前に取るこれらの「遅れ」変数の100を作成します。私は新しい変数名を100回タイプすることなくこれを行う方法を理解しようとしていますが、私は近づいています。

例では、thisの例とthisの例を調べました。それらは似ていますが、私が必要とする特定の解決策をまとめてまとめることはできません。どんな助けもありがとう!


インスピレーションのために@BenFasoliに感謝します。私は彼の答えを取って、私が必要とする解決策を得るためにちょっと微調整しました。 This RStudio BlogThis SO postも使用しました。変数名の「遅れ」は先行するのではなく後続していますが、私はそれで生きることができます。

私の最終的なコードは、それが他の誰にも役に立ちます場合にはここに掲載されて:あなたは、列名にlag_を付加(特定の列またはmutate_atmutate_allを使用することができます

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> 

答えて

3

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 
+0

感謝を。私はあなたの答えをとり、私が必要とする解決策を得るためにちょっと微調整しました。私も[This RStudio Blog](https://blog.rstudio.org/2016/06/27/dplyr-0-5-0/)と[This SO post](http://stackoverflow.com/questions)を使いました。/27027347/mutate-each-summarize-each-in-dplyr-how-do-i-select-certain-columns-and-give)である。 私の最終的なコードは、それが他の誰にも役に立ちます場合にはここに掲載されています '遅れ<- iris %>% GROUP_BY(種)%>% mutate_at( VARS(Sepal.Length:Petal.Length)、 低速運行(「遅れ"=遅れ))%>% ungroup' もう一度ありがとう! –

2

ここではdata.tableアプローチです。私はこの場合に数字の列を選んだ。あなたがしたいのは、列名を選択して、事前に新しい列名を作成することです。次に、dplyrパッケージにlag()lead()のように動作するshift()を、選択した各列に適用します。あなたはまた、非dplyrに満足しているので

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 
1

、これを試してみてください:インスピレーションのための

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 
関連する問題