I have a tibble
and a named vector. I would like to make copies of all of the columns in my named vector using the vector names while preserving the original names.
I know how to rename all the columns fairly simply:
library(dplyr)
named_vector <-
c("var1" = "x1",
"var2" = "x2",
"var3" = "x3")
tibble(x1 = 1:3, x2 = 1:3, x3 = 1:3, z = 68, zz = 69) %>%
rename(!!!named_vector)
#> # A tibble: 3 x 5
#> var1 var2 var3 z zz
#> <int> <int> <int> <dbl> <dbl>
#> 1 1 1 1 68 69
#> 2 2 2 2 68 69
#> 3 3 3 3 68 69
Created on 2021-08-27 by the reprex package (v0.3.0)
But I don't know how to do the equivalent using mutate
. How can I make copies of the columns in a way that preserves the original names and but also has the vector names?
My expected output would be the equivalent of:
library(dplyr)
named_vector <-
c("var1" = "x1",
"var2" = "x2",
"var3" = "x3")
tibble(x1 = 1:3, x2 = 1:3, x3 = 1:3, z = 68, zz = 69) %>%
mutate(var1 = x1,
var2 = x2,
var3 = x3)
#> # A tibble: 3 x 8
#> x1 x2 x3 z zz var1 var2 var3
#> <int> <int> <int> <dbl> <dbl> <int> <int> <int>
#> 1 1 1 1 68 69 1 1 1
#> 2 2 2 2 68 69 2 2 2
#> 3 3 3 3 68 69 3 3 3
Created on 2021-08-27 by the reprex package (v0.3.0)
In R programming, the mutate function is used to create a new variable from a data set. In order to use the function, we need to install the dplyr package, which is an add-on to R that includes a host of cool functions for selecting, filtering, grouping, and arranging data.
You can use the mutate() function from the dplyr package to add one or more columns to a data frame in R.
How to add a new column to a data frame using mutate in R? How to add a new column to a data frame using mutate in R? The mutate function of dplyr package in R can help us to add a new column to a data frame and the benefit of using mutate is that we can decide the position of the new column during the addition.
To use mutate in R, all you need to do is call the function, specify the dataframe, and specify the name-value pair for the new variable you want to create. Example: how to use mutate in R The explanation I just gave is pretty straightforward, but to make it more concrete, let’s work with some actual data.
When you use mutate (), you’re basically creating a variable. The new variable needs a name, but it also needs a value that gets assigned to that name. So when you use mutate, you provide the name and the new value … a name-value pair. You can see here in this dummy code example that we’re creating a new variable called new_variable.
Create or transform variables. mutate() adds new variables and preserves existing ones; transmute() adds new variables and drops existing ones. Both functions preserve the number of rows of the input. New variables overwrite existing variables of the same name.
We may use across
with mutate
, and rename with str_replace
by replacing the substring 'x' with 'var' to create new columns
library(dplyr)
library(stringr)
tibble(x1 = 1:3, x2 = 1:3, x3 = 1:3) %>%
mutate(across(everything(),
.names = "{str_replace(.col, 'x', 'var')}"))
-output
# A tibble: 3 x 6
x1 x2 x3 var1 var2 var3
<int> <int> <int> <int> <int> <int>
1 1 1 1 1 1 1
2 2 2 2 2 2 2
3 3 3 3 3 3 3
Or use match
to named_vector in .names
tibble(x1 = 1:3, x2 = 1:3, x3 = 1:3) %>%
mutate(across(all_of(unname(named_vector)),
.names = "{names(named_vector)[match(.col, named_vector)]}"))
-output
# A tibble: 3 x 6
x1 x2 x3 var1 var2 var3
<int> <int> <int> <int> <int> <int>
1 1 1 1 1 1 1
2 2 2 2 2 2 2
3 3 3 3 3 3 3
With the updated post also the solution works
tibble(x1 = 1:3, x2 = 1:3, x3 = 1:3, z = 68, zz = 69) %>%
mutate(across(all_of(unname(named_vector)),
.names = "{names(named_vector)[match(.col, named_vector)]}"))
# A tibble: 3 x 8
x1 x2 x3 z zz var1 var2 var3
<int> <int> <int> <dbl> <dbl> <int> <int> <int>
1 1 1 1 68 69 1 1 1
2 2 2 2 68 69 2 2 2
3 3 3 3 68 69 3 3 3
You can create new columns from existing columns using -
data <- tibble::tibble(x1 = 1:3, x2 = 1:3, x3 = 1:3, z = 68, zz = 69)
data[names(named_vector)] <- data[named_vector]
data
# x1 x2 x3 z zz var1 var2 var3
# <int> <int> <int> <dbl> <dbl> <int> <int> <int>
#1 1 1 1 68 69 1 1 1
#2 2 2 2 68 69 2 2 2
#3 3 3 3 68 69 3 3 3
The new variable names will be taken from the named vector just by doing:
library(dplyr)
tibble(x1 = 1:3, x2 = 4:6, x3 = 7:9, z = 68, zz = 69) %>%
mutate(across(all_of(named_vector)))
# A tibble: 3 x 8
x1 x2 x3 z zz var1 var2 var3
<int> <int> <int> <dbl> <dbl> <int> <int> <int>
1 1 1 1 68 69 1 1 1
2 2 2 2 68 69 2 2 2
3 3 3 3 68 69 3 3 3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With