【问题标题】:mutate_each / summarise_each in dplyr: how do I select certain columns and give new names to mutated columns?dplyr 中的 mutate_each / summarise_each:如何选择某些列并为变异列赋予新名称?
【发布时间】:2015-01-17 14:11:18
【问题描述】:

我对 dplyr 动词 mutate_each. 有点困惑

使用基本的mutate 将一列数据转换为z-scores 并在您的data.frame 中创建一个新列(此处名称为z_score_data)非常简单:

newDF <- DF %>%
  select(one_column) %>%
  mutate(z_score_data = one_column - (mean(one_column) / sd(one_column))

但是,由于我要转换许多数据列,看来我应该使用 mutate_each 动词。

newDF <- DF %>%
     mutate_each(funs(scale))

到目前为止一切顺利。但到目前为止我还没有弄清楚:

  1. 如何为这些新列指定合适的名称,就像在 mutate 中一样?
  2. 如何选择我希望改变的某些列,就像我在第一种情况下对 select 所做的那样?

感谢您的帮助。

【问题讨论】:

    标签: r dataframe dplyr


    【解决方案1】:

    dplyr 更新 >= 0.4.3.9000

    在 dplyr 开发版本 0.4.3.9000(撰写本文时)中,mutate_eachsummarise_each 内部的命名已简化,如 News 中所述:

    summarise_each()mutate_each() 的命名行为已 进行了调整,以便您可以强制包含函数和 变量名:summarise_each(mtcars, funs(mean = mean), everything())

    如果您只想在 mutate_each / summarise_each 中应用 1 个函数并且您想为这些列赋予新名称,这一点非常重要。

    为了显示差异,这里是使用新命名功能的 dplyr 0.4.3.9000 的输出,与下面的选项 a.2 形成对比:

    library(dplyr) # >= 0.4.3.9000
    iris %>% mutate_each(funs(mysum = sum(.)), -Species) %>% head()
    #  Sepal.Length Sepal.Width Petal.Length Petal.Width Species Sepal.Length_mysum Sepal.Width_mysum
    #1          5.1         3.5          1.4         0.2  setosa              876.5             458.6
    #2          4.9         3.0          1.4         0.2  setosa              876.5             458.6
    #3          4.7         3.2          1.3         0.2  setosa              876.5             458.6
    #4          4.6         3.1          1.5         0.2  setosa              876.5             458.6
    #5          5.0         3.6          1.4         0.2  setosa              876.5             458.6
    #6          5.4         3.9          1.7         0.4  setosa              876.5             458.6
    #  Petal.Length_mysum Petal.Width_mysum
    #1              563.7             179.9
    #2              563.7             179.9
    #3              563.7             179.9
    #4              563.7             179.9
    #5              563.7             179.9
    #6              563.7             179.9
    

    如果您不提供新名称并且只提供 1 个函数,dplyr 将更改现有列(就像在以前的版本中所做的那样):

    iris %>% mutate_each(funs(sum), -Species) %>% head()
    #  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
    #1        876.5       458.6        563.7       179.9  setosa
    #2        876.5       458.6        563.7       179.9  setosa
    #3        876.5       458.6        563.7       179.9  setosa
    #4        876.5       458.6        563.7       179.9  setosa
    #5        876.5       458.6        563.7       179.9  setosa
    #6        876.5       458.6        563.7       179.9  setosa
    

    我假设这个新功能将在下一个版本 0.4.4 中通过 CRAN 提供。


    dplyr 版本

    我怎样才能给这些新列适当的名称,就像我可以在 变异?

    a) 1 个函数应用于mutate_each/summarise_each

    如果您在 mutate_eachsummarise_each 中仅应用 1 个函数,则现有列将被转换并且名称将保持原样,除非您向 mutate_each_ 提供命名向量/summarise_each_(见选项 a.4)

    这里有一些例子:

    a.1 只有 1 个函数 -> 将保留现有名称

    iris %>% mutate_each(funs(sum), -Species) %>% head()
    #  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
    #1          876         459          564         180  setosa
    #2          876         459          564         180  setosa
    #3          876         459          564         180  setosa
    #4          876         459          564         180  setosa
    #5          876         459          564         180  setosa
    #6          876         459          564         180  setosa
    

    a.2 如果你指定一个新的列扩展名:

    iris %>% mutate_each(funs(mysum = sum(.)), -Species) %>% head()
    #  Sepal.Length Sepal.Width Petal.Length Petal.Width Species
    #1          876         459          564         180  setosa
    #2          876         459          564         180  setosa
    #3          876         459          564         180  setosa
    #4          876         459          564         180  setosa
    #5          876         459          564         180  setosa
    #6          876         459          564         180  setosa
    

    a.3 手动为每列指定一个新名称(但仅适用于少数列):

    iris %>% mutate_each(funs(sum), SLsum = Sepal.Length,SWsum = Sepal.Width,  -Species) %>% head()
    #  Sepal.Length Sepal.Width Petal.Length Petal.Width Species SLsum SWsum
    #1          5.1         3.5          1.4         0.2  setosa   876   459
    #2          4.9         3.0          1.4         0.2  setosa   876   459
    #3          4.7         3.2          1.3         0.2  setosa   876   459
    #4          4.6         3.1          1.5         0.2  setosa   876   459
    #5          5.0         3.6          1.4         0.2  setosa   876   459
    #6          5.4         3.9          1.7         0.4  setosa   876   459
    

    a.4 使用命名向量创建具有新名称的附加列:

    案例 1:保留原始列

    与选项 a.1、a.2 和 a.3 相比,dplyr 将保持现有列不变并以这种方法创建新列。新列的名称等于您预先创建的命名向量的名称(在本例中为vars)。

    vars <- names(iris)[1:2]  # choose which columns should be mutated
    vars <- setNames(vars, paste0(vars, "_sum")) # create new column names
    iris %>% mutate_each_(funs(sum), vars) %>% head 
    #  Sepal.Length Sepal.Width Petal.Length Petal.Width Species Sepal.Length_sum Sepal.Width_sum
    #1          5.1         3.5          1.4         0.2  setosa            876.5           458.6
    #2          4.9         3.0          1.4         0.2  setosa            876.5           458.6
    #3          4.7         3.2          1.3         0.2  setosa            876.5           458.6
    #4          4.6         3.1          1.5         0.2  setosa            876.5           458.6
    #5          5.0         3.6          1.4         0.2  setosa            876.5           458.6
    #6          5.4         3.9          1.7         0.4  setosa            876.5           458.6
    

    案例 2:删除原始列

    如您所见,这种方法保持现有列不变,并添加具有指定名称的新列。如果您不想保留原始列,而只想保留新创建的列(和其他列),您可以在之后添加 select 语句:

    iris %>% mutate_each_(funs(sum), vars) %>% select(-one_of(vars)) %>% head
    #  Petal.Length Petal.Width Species Sepal.Length_sum Sepal.Width_sum
    #1          1.4         0.2  setosa            876.5           458.6
    #2          1.4         0.2  setosa            876.5           458.6
    #3          1.3         0.2  setosa            876.5           458.6
    #4          1.5         0.2  setosa            876.5           458.6
    #5          1.4         0.2  setosa            876.5           458.6
    #6          1.7         0.4  setosa            876.5           458.6
    

    b) 在mutate_each/summarise_each 中应用了多个函数

    b.1 让 dplyr 找出新名称

    如果你应用了超过 1 个函数,你可以让 dplyr 自己找出名字(它会保留现有的列):

    iris %>% mutate_each(funs(sum, mean), -Species) %>% head()
    #  Sepal.Length Sepal.Width Petal.Length Petal.Width Species Sepal.Length_sum Sepal.Width_sum Petal.Length_sum
    #1          5.1         3.5          1.4         0.2  setosa              876             459              564
    #2          4.9         3.0          1.4         0.2  setosa              876             459              564
    #3          4.7         3.2          1.3         0.2  setosa              876             459              564
    #4          4.6         3.1          1.5         0.2  setosa              876             459              564
    #5          5.0         3.6          1.4         0.2  setosa              876             459              564
    #6          5.4         3.9          1.7         0.4  setosa              876             459              564
    #  Petal.Width_sum Sepal.Length_mean Sepal.Width_mean Petal.Length_mean Petal.Width_mean
    #1             180              5.84             3.06              3.76              1.2
    #2             180              5.84             3.06              3.76              1.2
    #3             180              5.84             3.06              3.76              1.2
    #4             180              5.84             3.06              3.76              1.2
    #5             180              5.84             3.06              3.76              1.2
    #6             180              5.84             3.06              3.76              1.2
    

    b.2 手动指定新列名

    当使用超过 1 个函数时,另一种选择是自行指定列扩展名:

    iris %>% mutate_each(funs(MySum = sum(.), MyMean = mean(.)), -Species) %>% head()
    #  Sepal.Length Sepal.Width Petal.Length Petal.Width Species Sepal.Length_MySum Sepal.Width_MySum Petal.Length_MySum
    #1          5.1         3.5          1.4         0.2  setosa                876               459                564
    #2          4.9         3.0          1.4         0.2  setosa                876               459                564
    #3          4.7         3.2          1.3         0.2  setosa                876               459                564
    #4          4.6         3.1          1.5         0.2  setosa                876               459                564
    #5          5.0         3.6          1.4         0.2  setosa                876               459                564
    #6          5.4         3.9          1.7         0.4  setosa                876               459                564
    #  Petal.Width_MySum Sepal.Length_MyMean Sepal.Width_MyMean Petal.Length_MyMean Petal.Width_MyMean
    #1               180                5.84               3.06                3.76                1.2
    #2               180                5.84               3.06                3.76                1.2
    #3               180                5.84               3.06                3.76                1.2
    #4               180                5.84               3.06                3.76                1.2
    #5               180                5.84               3.06                3.76                1.2
    #6               180                5.84               3.06                3.76                1.2
    

    我如何选择我希望改变的某些列,就像我做的那样 在第一种情况下选择?

    您可以通过在此处给出名称(mutate Sepal.Length,但不是 Species)来引用要变异(或忽略)的列:

    iris %>% mutate_each(funs(sum), Sepal.Length, -Species) %>% head()
    

    此外,您可以使用特殊功能选择要变异的列,所有以某个单词开头或包含某个单词的列等,例如:

    iris %>% mutate_each(funs(sum), contains("Sepal"),  -Species) %>% head()
    

    有关这些功能的更多信息,请参阅?mutate_each?select

    评论后编辑1:

    如果您想使用标准评估,dplyr 提供大多数以附加“_”结尾的函数的 SE 版本。所以在这种情况下,你会使用:

    x <- c("Sepal.Width", "Sepal.Length") # vector of column names 
    iris %>% mutate_each_(funs(sum), x) %>% head()
    

    注意我在这里使用的mutate_each_


    编辑 2:使用选项 a.4 更新

    【讨论】:

    • 谢谢,这太棒了。如果我可以如此大胆,最后一件事 - 有什么方法可以简单地覆盖我的数据帧而不是将输出分配给新的数据帧?如DF &lt;- DF %&gt;% etc etc?
    • 不,您可以将新的/变异的 data.frame 分配给相同的对象名称,例如iris &lt;- iris %&gt;% mutate_each_(funs(sum), x) %&gt;% head() 将更改 iris 数据集。如果您不希望这样做,另一种选择是将其分配给新的 data.frame,例如myiris &lt;- iris %&gt;% mutate_each_(funs(sum), x) %&gt;% head()。两种选择都可以
    • @MattO'Brien 有点晚了,但对于任何关注这些 cmets 的人来说:df %&lt;&gt;% mutate(...) %&gt;% ... 作品,目前在 magrittr 主页底部附近提到:github.com/smbache/magrittr
    • @kennyB,我已经更新了我的答案。查看选项 a.4,即使用提供给 mutate_each_summarise_each_ 的命名向量。
    • @ML_Passion,你可以这样做:mtcars %&gt;% mutate_all(funs(decile = ntile(., 10)))
    【解决方案2】:

    mutate_each 将被弃用,考虑使用mutate_at。来自dplyr_0.5.0 文档:

    未来 mutate_each() 和 summarise_each() 将被弃用,取而代之的是功能更丰富的函数系列:mutate_all()、mutate_at()、mutate_if()、summarise_all()、summarise_at() 和 summarise_if()。

    对除Species以外的所有变量应用函数:

    警告:'.cols' 参数已弃用,请参阅底部的注释!

    iris %>% mutate_at(.cols=vars(-Species), .funs=funs(mysum = sum(.))) %>% head()
    
     Sepal.Length Sepal.Width Petal.Length Petal.Width Species Sepal.Length_mysum Sepal.Width_mysum
    1          5.1         3.5          1.4         0.2  setosa              876.5             458.6
    2          4.9         3.0          1.4         0.2  setosa              876.5             458.6
    3          4.7         3.2          1.3         0.2  setosa              876.5             458.6
    4          4.6         3.1          1.5         0.2  setosa              876.5             458.6
    5          5.0         3.6          1.4         0.2  setosa              876.5             458.6
    6          5.4         3.9          1.7         0.4  setosa              876.5             458.6
      Petal.Length_mysum Petal.Width_mysum
    1              563.7             179.9
    2              563.7             179.9
    3              563.7             179.9
    4              563.7             179.9
    5              563.7             179.9
    6              563.7             179.9
    

    将函数应用于变量子集

    vars_to_process=c("Petal.Length","Petal.Width")
    iris %>% mutate_at(.cols=vars_to_process, .funs=funs(mysum = sum(.))) %>% head()
    
     Sepal.Length Sepal.Width Petal.Length Petal.Width Species Petal.Length_mysum Petal.Width_mysum
    1          5.1         3.5          1.4         0.2  setosa              563.7             179.9
    2          4.9         3.0          1.4         0.2  setosa              563.7             179.9
    3          4.7         3.2          1.3         0.2  setosa              563.7             179.9
    4          4.6         3.1          1.5         0.2  setosa              563.7             179.9
    5          5.0         3.6          1.4         0.2  setosa              563.7             179.9
    6          5.4         3.9          1.7         0.4  setosa              563.7             179.9
    

    更新!适用于 dplyr 0.7.1 版本 (2017-08-08)

    如果您看到消息:

    .cols 已重命名并已弃用,请使用.vars

    然后将.cols 更改为.vars

    iris %>% mutate_at(.vars=vars(-Species), .funs=funs(mysum = sum(.))) %>% head()
    

    另一个例子:

    iris %>% mutate_at(.vars=vars(Sepal.Width), .funs=funs(mysum = sum(.))) %>% head()
    

    相当于:

    iris %>% mutate_at(.vars=vars("Sepal.Width"), .funs=funs(mysum = sum(.))) %>% head()
    

    此外,在此版本中,mutate_each 已弃用:

    mutate_each() 已弃用。 请改用mutate_all()mutate_at()mutate_if()。 要将funs 映射到选择的变量上,请使用mutate_at()

    【讨论】:

      猜你喜欢
      • 2015-10-26
      • 1970-01-01
      • 2021-05-07
      • 1970-01-01
      • 2018-01-30
      • 2019-01-17
      • 2015-09-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多