You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在函数内部使用tidyverse的mutate实现基于列名的新列创建

Got it, let's work through this together. You're looking to convert your base R function f into a tidyverse-style function (f.tidy) that takes a data frame and a string for the new column name, then adds that column using existing data—no hardcoded column names, right?

First, let's start with your example data to ground this:

df <- data.frame('test' = 1:3, 'tcy' = 4:6)

Which gives us this small data frame:

testtcy
14
25
36

I’m guessing your base R function f looks something like this (feel free to tweak if I’m off the mark):

# Example base R function you already have
f <- function(df, new_col) {
  df[[new_col]] <- df$test + df$tcy
  df
}

# Usage: adds a new column with the string name you pass
f(df, "test_plus_tcy")

Now, for the tidyverse version, the key challenge is handling the dynamic column name (since it’s a string input, not a bare column name). Tidyverse has two solid, readable ways to solve this—let’s break them down:

Method 1: Classic Tidy Evaluation with sym() and !!

We first convert the input string to a "symbol" (the tidyverse’s way of referencing column names), then use the unquote operator (!!) to tell mutate to use that symbol as the new column name. We also need the walrus operator (:=) instead of = because we’re assigning to a dynamic name:

library(tidyverse)

f.tidy <- function(df, new_col_name) {
  df %>%
    mutate(!!sym(new_col_name) := test + tcy)
}

# Test it out
f.tidy(df, "test_plus_tcy")

Method 2: Modern Tidyverse Style with ensym() and {{ }}

ensym() automatically converts your string input to a symbol, and the curly-curly operator ({{ }}) handles unquoting in a cleaner, more intuitive way:

f.tidy <- function(df, new_col_name) {
  new_col <- ensym(new_col_name)
  df %>%
    mutate({{ new_col }} := test + tcy)
}

# Same usage, same result
f.tidy(df, "test_plus_tcy")

Key Notes to Remember:

  • The := operator is non-negotiable here—regular = won’t work when assigning to a dynamic column name in mutate.
  • If your new column’s calculation is more complex than test + tcy, just swap that part out with your actual logic (e.g., log(test) * tcy or any other operation using existing columns).
  • If you ever want to pass the new column name without quotes (like f.tidy(df, test_plus_tcy) instead of the string), you can skip the sym()/ensym() step and just use {{ new_col_name }} := ... directly. But since you specified the input is a string, the above methods are exactly what you need.

When you run either version, you’ll get this output—matching exactly what your base R function does, but in that clean tidyverse pipe flow:

test tcy test_plus_tcy
1    1   4             5
2    2   5             7
3    3   6             9

内容的提问来源于stack exchange,提问作者Tyler Law

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:15:45