I am trying to create a new variable in an R dataframe using the Base R pipe and within(). If I specify the new variable’s name directly, it works just fine. But if the new variable’s name is the value of another variable, my code doesn’t work
Minimal example:
NEW_NAME <- "c"
df <- data.frame(a = 1:3,
b = 4:6)
df |>
within(c <- a+b)
This works beautifully:
> df |>
within(c <- a+b)
a b c
1 1 4 5
2 2 5 7
3 3 6 9
But if I try and pick up the new column name from NEW_NAME, I run into trouble:
df |>
within(get(NEW_NAME) <- a+b)
Error in get(NEW_NAME) <- a + b : could not find function "get<-"
I have tried within(.NEW_NAME <- a + b)
, within(~NEW_NAME <- a + b)
as well as various other things that have not worked. What is the correct syntax for this?
Sincerely
Thomas Philips
1
Keep in mind that df |> within(get(NEW_NAME) <- a+b)
is just another way to write within(df, get(NEW_NAME) <- a+b)
which also doesn’t work. The pipe is base R is just a syntax shortcut. It doesn’t change how functions work or variables are expanded.
When using base R, you use get()
and assign()
when working with variables names as strings. So if you want to assign a value, you would need to use
df |>
within(assign(NEW_NAME, a+b))
You could create your own type of assignment function helper that would work like
`:=` <- function(x, value) {
assign(x, value, parent.frame())
}
df |>
within(NEW_NAME := a+b)
# a b c
# 1 1 4 5
# 2 2 5 7
# 3 3 6 9
Though I probably wouldn’t recommend that because it’s even more non-standard.
You can use assign
!
NEW_NAME <- "c"
df <- data.frame(a = 1:3,
b = 4:6)
df |>
within(assign(NEW_NAME, a+b))
#> a b c
#> 1 1 4 5
#> 2 2 5 7
#> 3 3 6 9
or in dplyr
. Updated: See @MrFlick comment
df |>
dplyr::mutate("{NEW_NAME}" := a+b)
#> a b c
#> 1 1 4 5
#> 2 2 5 7
#> 3 3 6 9
3