Converting Two Columns Of A Data Frame To A Named Vector
Answer :
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 also use deframe(x) from the tibble package for this.
tibble::deframe()
It converts the first column to names and second column to values.
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))
Comments
Post a Comment