'Assigning values from data frame to vector in R [duplicate]

I have a data frame as below:

data <- data.frame(name = c("a", "b"),
                   value = c("apple", "ball"))

Now from this data I want to create a vector as follows:

transformed_data <- c( a = "apple", b = "ball") 

How this is possible to do in R?



Solution 1:[1]

Since you're tagging tidyverse in the question, you can use deframe from the tibble package:

tibble::deframe(data)

      a       b 
"apple"  "ball" 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Stefano Barbi