将数据框架的两列转换为命名向量

我需要将一个多行两列的 data.frame转换为一个命名的字符向量。 我的 data.frame是这样的:

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

我真正需要的是:

whatiwant = c("a" = "Alpha",
"b" = "Beta",
"c" = "Caesar",
"d" = "Doris")
26711 次浏览

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)

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))

For variety, try split and unlist:

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

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.

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.

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".