I would like to use the variable "second_column" as a variable to refer to the column "test1$b". I tried different things, but couldn't find any solutions yet. test1 and test2 should be the same in the end...
test1 <- data.frame(a = 1:5, b = 6:10)
second_column = "b"
test2 <- test1 %>%
transmute(variable1 = a,
variable2 = second_column
)
test1
test2
I would suggest using !!
from rlang
and as.name()
. These two elements are useful for the kind of evaluation you want in order to create your variables:
#Data
test1 <- data.frame(a = 1:5, b = 6:10)
#Var name
second_column = "b"
#Data 2
test2 <- test1 %>%
transmute(variable1 = a,
variable2 = !!as.name(second_column)
)
#Dataframes
test1
test2
Output:
test1
a b
1 1 6
2 2 7
3 3 8
4 4 9
5 5 10
test2
variable1 variable2
1 1 6
2 2 7
3 3 8
4 4 9
5 5 10
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments