使用 dplyr mutate 自动生成新的变量名 [英] Automatically generate new variable names using dplyr mutate

查看:39
本文介绍了使用 dplyr mutate 自动生成新的变量名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在使用 dplyr 时动态创建变量名;不过,我也可以使用非 dplyr 解决方案.

I would like to create variable names dynamically while using dplyr; although, I’d be fine with a non-dplyr solution as well.

例如:

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 个这样的滞后"变量,而不是这样做 3 次,它们的名称为:lag_original 变量名.我正在尝试弄清楚如何在不输入新变量名称 100 次的情况下执行此操作,但我的方法很短.

But, instead of doing this three times, I want to create 100 of these "lag" variables that take in the name: lag_original variable name. I’m trying to figure out how to do this without typing the new variable name 100 times, but I’m coming up short.

我研究了这个示例和this 其他地方的示例.它们很相似,但我不太能拼凑出我需要的特定解决方案.任何帮助表示赞赏!

I’ve looked into this example and this example elsewhere on SO. They are similar, but I’m not quite able to piece together the specific solution I need. Any help is appreciated!

编辑
感谢@BenFasoli 的启发.我接受了他的回答并稍微调整了一下以获得我需要的解决方案.我还使用了 这个 RStudio 博客这篇文章.变量名称中的滞后"是尾随而不是前导,但我可以接受.

Edit
Thanks to @BenFasoli for the inspiration. I took his answer and tweaked it just a bit to get the solution I needed. I also used This RStudio Blog and This SO post. The "lag" in the variable name is trailing instead of leading, but I can live with that.

我的最终代码发布在这里,以防对其他人有帮助:

My final code is posted here in case it’s helpful to anyone else:

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>

推荐答案

您可以使用 mutate_all(或 mutate_at 用于特定列),然后添加 lag_ 到列名.

You can use mutate_all (or mutate_at for specific columns) then prepend lag_ to the column names.

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

这篇关于使用 dplyr mutate 自动生成新的变量名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆