0

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?

Williams86
  • 320
  • 1
  • 11

2 Answers2

3

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

tibble::deframe(data)

      a       b 
"apple"  "ball" 
Stefano Barbi
  • 2,978
  • 1
  • 12
  • 11
2

There's a tidyverse approach given in the comment, so I'll include a base R approach here:

setNames(data[["value"]], data[["name"]])

      a       b 
"apple"  "ball"
benson23
  • 16,369
  • 9
  • 19
  • 38