dplyr 更新 >= 0.4.3.9000
在 dplyr 开发版本 0.4.3.9000(撰写本文时)中,mutate_each 和 summarise_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_each 或 summarise_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 更新