dplyr-muate:使用动态变量名称

dplyr-muate:使用动态变量名称

我想用dplyr‘s mutate()若要在数据框架中创建多个新列,请执行以下操作。应该动态生成列名及其内容。

来自虹膜的示例数据:

require(dplyr)data(iris)iris <- tbl_df(iris)

我创建了一个函数来从Petal.Width变量:

multipetal <- function(df, n) {
    varname <- paste("petal", n , sep=".")
    df <- mutate(df, varname = Petal.Width * n)  ## problem arises here
    df}

现在我创建一个循环来构建我的列:

for(i in 2:5) {
    iris <- multipetal(df=iris, n=i)}

但是,由于muate认为varname是一个字面变量名,所以循环只创建一个新变量(称为varname),而不是创建4个变量(称为petal.2-petal.5)。

我怎么才能mutate()使用我的动态名称作为变量名?


有只小跳蛙
浏览 721回答 3
3回答

子衿沉夜

在新发布的dplyr&nbsp;(0.6.0等待2017年4月),我们也可以完成一项任务(:=)并通过取消引用(!!)不对其进行评估&nbsp;library(dplyr) &nbsp;multipetalN&nbsp;<-&nbsp;function(df,&nbsp;n){ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;varname&nbsp;<-&nbsp;paste0("petal.",&nbsp;n) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;df&nbsp;%>% &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mutate(!!varname&nbsp;:=&nbsp;Petal.Width&nbsp;*&nbsp;n) &nbsp;} &nbsp;data(iris) &nbsp;iris1&nbsp;<-&nbsp;tbl_df(iris) &nbsp;iris2&nbsp;<-&nbsp;tbl_df(iris) &nbsp;for(i&nbsp;in&nbsp;2:5)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;iris2&nbsp;<-&nbsp;multipetalN(df=iris2,&nbsp;n=i) &nbsp;}基于@MrFlick的输出检查multipetal在“iris 1”上应用identical(iris1,&nbsp;iris2)#[1]&nbsp;TRUE

白板的微信

这是另一个版本,可以说更简单一些。multipetal <- function(df, n) {&nbsp; &nbsp; varname <- paste("petal", n, sep=".")&nbsp; &nbsp; df<-mutate_(df, .dots=setNames(paste0("Petal.Width*",n), varname))&nbsp; &nbsp; df}for(i in 2:5) {&nbsp; &nbsp; iris <- multipetal(df=iris, n=i)}> head(iris)Sepal.Length Sepal.Width Petal.Length Petal.Width Species petal.2 petal.3 petal.4 petal.51&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 5.1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;3.5&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1.4&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0.2&nbsp; setosa&nbsp; &nbsp; &nbsp;0.4&nbsp; &nbsp; &nbsp;0.6&nbsp; &nbsp; &nbsp;0.8&nbsp; &nbsp; &nbsp; &nbsp;12&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 4.9&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;3.0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1.4&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0.2&nbsp; setosa&nbsp; &nbsp; &nbsp;0.4&nbsp; &nbsp; &nbsp;0.6&nbsp; &nbsp; &nbsp;0.8&nbsp; &nbsp; &nbsp; &nbsp;13&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 4.7&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;3.2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1.3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0.2&nbsp; setosa&nbsp; &nbsp; &nbsp;0.4&nbsp; &nbsp; &nbsp;0.6&nbsp; &nbsp; &nbsp;0.8&nbsp; &nbsp; &nbsp; &nbsp;14&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 4.6&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;3.1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1.5&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0.2&nbsp; setosa&nbsp; &nbsp; &nbsp;0.4&nbsp; &nbsp; &nbsp;0.6&nbsp; &nbsp; &nbsp;0.8&nbsp; &nbsp; &nbsp; &nbsp;15&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 5.0&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;3.6&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1.4&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0.2&nbsp; setosa&nbsp; &nbsp; &nbsp;0.4&nbsp; &nbsp; &nbsp;0.6&nbsp; &nbsp; &nbsp;0.8&nbsp; &nbsp; &nbsp; &nbsp;16&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 5.4&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;3.9&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1.7&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0.4&nbsp; setosa&nbsp; &nbsp; &nbsp;0.8&nbsp; &nbsp; &nbsp;1.2&nbsp; &nbsp; &nbsp;1.6&nbsp; &nbsp; &nbsp; &nbsp;2
打开App,查看更多内容
随时随地看视频慕课网APP