Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a named vector using dplyr

Tags:

r

dplyr

vector

I am trying to find a way to create a named vector from two columns in a data frame (one of values, one of names) using pipes. Thus far I have the following (using mtcars as example data)...

library(tidyverse)

x <- mtcars %>%
  rownames_to_column("car") %>%
  select(car, mpg)
  pull(mpg)

names(x) <- row.names(mtcars)

x
#            Mazda RX4       Mazda RX4 Wag          Datsun 710      Hornet 4 Drive   Hornet Sportabout 
#                 21.0                21.0                22.8                21.4                18.7 
#              Valiant          Duster 360           Merc 240D            Merc 230            Merc 280 
#                 18.1                14.3                24.4                22.8                19.2 
#            Merc 280C          Merc 450SE          Merc 450SL         Merc 450SLC  Cadillac Fleetwood 
#                 17.8                16.4                17.3                15.2                10.4 
#  Lincoln Continental   Chrysler Imperial            Fiat 128         Honda Civic      Toyota Corolla 
#                 10.4                14.7                32.4                30.4                33.9 
#        Toyota Corona    Dodge Challenger         AMC Javelin          Camaro Z28    Pontiac Firebird 
#                 21.5                15.5                15.2                13.3                19.2 
#            Fiat X1-9       Porsche 914-2        Lotus Europa      Ford Pantera L        Ferrari Dino 
#                 27.3                26.0                30.4                15.8                19.7 
#        Maserati Bora          Volvo 142E 
#                 15.0                21.4 

This is not very convenient when it comes to my actual data, as I want to avoid saving a copy of the transformed data frame to only use the column of names for the name function.

like image 656
guyabel Avatar asked Mar 17 '18 11:03

guyabel


Video Answer


2 Answers

Since tibble 1.3.0 (2017-04-02), you can use tibble::deframe() :

library(tidyverse)

mtcars %>%
  rownames_to_column("car") %>%
  select(car, mpg) %>% 
  deframe()

          Mazda RX4       Mazda RX4 Wag          Datsun 710 
               21,0                21,0                22,8 
     Hornet 4 Drive   Hornet Sportabout             Valiant 
               21,4                18,7                18,1 
         Duster 360           Merc 240D            Merc 230 
               14,3                24,4                22,8 
           Merc 280           Merc 280C          Merc 450SE 
               19,2                17,8                16,4 
         Merc 450SL         Merc 450SLC  Cadillac Fleetwood 
               17,3                15,2                10,4 
Lincoln Continental   Chrysler Imperial            Fiat 128 
               10,4                14,7                32,4 
        Honda Civic      Toyota Corolla       Toyota Corona 
               30,4                33,9                21,5 
   Dodge Challenger         AMC Javelin          Camaro Z28 
               15,5                15,2                13,3 
   Pontiac Firebird           Fiat X1-9       Porsche 914-2 
               19,2                27,3                26,0 
       Lotus Europa      Ford Pantera L        Ferrari Dino 
               30,4                15,8                19,7 
      Maserati Bora          Volvo 142E 
               15,0                21,4 
like image 135
mdag02 Avatar answered Oct 18 '22 23:10

mdag02


We can use the names<- to get a named vector

library(tidyverse)
mtcars %>% 
   rownames_to_column("car") %>%
   {'names<-'(.$mpg, .$car)}

Or with set_names

mtcars %>% 
   rownames_to_column("car") %>% 
   select(x = mpg, nm = car) %>%
   pmap(set_names) %>% 
   unlist
like image 6
akrun Avatar answered Oct 18 '22 23:10

akrun