Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Create a data frame from vector, using the values as columns

Is there a convenient way to create a data frame from a vector and using the values of the vector as column names? Result should only have 1 row with empty fields. Values in vector may vary.

Given:

x <- c("1","2","3")

Output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

# A tibble: 1 x 3
  `1`   `2`   `3`  
  <chr> <chr> <chr>
1 ""    ""    ""  

>Solution :

If you’re a tidyverse fan as I see by your using of tibble, you could use a combination of map_dfc and setNames:

library(tidyverse)

df <- x %>% map_dfc(setNames, object = list(character(1)))

df
# A tibble: 1 x 3
 `1`   `2`   `3`  
 <chr> <chr> <chr>
1 ""    ""    "" 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading