Converting two columns of a data frame to a named vector

RVectorDataframeCoercion

R Problem Overview


I need to convert a multi-row two-column data.frame to a named character vector. My data.frame would be something like:

dd = data.frame(crit = c("a","b","c","d"), 
                name = c("Alpha", "Beta", "Caesar", "Doris")
                )

and what I actually need would be:

whatiwant = c("a" = "Alpha",
              "b" = "Beta",
              "c" = "Caesar",
              "d" = "Doris")

R Solutions


Solution 1 - R

Use the names function:

whatyouwant <- as.character(dd$name)
names(whatyouwant) <- dd$crit

as.character is necessary, because data.frame and read.table turn characters into factors with default settings.

If you want a one-liner:

whatyouwant <- setNames(as.character(dd$name), dd$crit)

Solution 2 - R

You can also use deframe(x) from the tibble package for this.

tibble::deframe()

It converts the first column to names and second column to values.

Solution 3 - R

You can make a vector from dd$name, and add names using names(), but you can do it all in one step with structure():

whatiwant <- structure(as.character(dd$name), names = as.character(dd$crit))

Solution 4 - R

Here is a very general, easy, tidy way:

library(dplyr)

iris %>%
  pull(Sepal.Length, Species)

The first argument is the values, the second argument is the names.

Solution 5 - R

For variety, try split and unlist:

unlist(split(as.character(dd$name), dd$crit))
#        a        b        c        d 
#  "Alpha"   "Beta" "Caesar"  "Doris" 

Solution 6 - R

There's also a magrittr solution to this via the exposition pipe (%$%):

library(magrittr)

dd %$% set_names(as.character(name), crit)

Minor advantage over tibble::deframe is that one doesn't have to have exactly a two-column frame/tibble as argument (i.e., avoid a select(value_col, name_col) %>%).

Note that the magrittr::set_names versus base::setNames is exchangeable. I simply prefer the former since it matches "set_(col|row)?names".

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionStefan FView Question on Stackoverflow
Solution 1 - RRolandView Answer on Stackoverflow
Solution 2 - RJohn WallerView Answer on Stackoverflow
Solution 3 - RalexwhanView Answer on Stackoverflow
Solution 4 - RstevecView Answer on Stackoverflow
Solution 5 - RA5C1D2H2I1M1N2O1R2T1View Answer on Stackoverflow
Solution 6 - RmervView Answer on Stackoverflow