I have a dataframe:
library(tidyverse)
df <- tribble(~col1, ~col2, 1, 2)
现在我想创建一个列。我有新列的名称字符串。它的实现方式如下:
df %>%
mutate("col3" = 3)
# A tibble: 1 x 3
col1 col2 col3
<dbl> <dbl> <dbl>
1 1 2 3
但实际情况并非如此:
newColumnName <- "col3"
df %>%
mutate(newColumnName = 3)
# A tibble: 1 x 3
col1 col2 newColumnName
<dbl> <dbl> <dbl>
1 1 2 3
我该如何创建一个新列,使其名称来自于一个对象中的字符串?