Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using `mutate` to create column copies using a named vector

Tags:

r

dplyr

names

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)

like image 473
John-Henry Avatar asked Aug 27 '21 20:08

John-Henry


People also ask

What does mutate() in R do?

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.

Does mutate create a new column in R?

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?

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.

How to use mutate in R?

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.

What does mutate () do in C++?

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.

What is the difference between mutate and transmute in SQL?

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.


3 Answers

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
like image 186
akrun Avatar answered Oct 28 '22 19:10

akrun


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
like image 24
Ronak Shah Avatar answered Oct 28 '22 19:10

Ronak Shah


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
like image 26
Ritchie Sacramento Avatar answered Oct 28 '22 21:10

Ritchie Sacramento