115

I have a named character vector returned from xmlAttrs like this:

testVect <- structure(c("11.2.0.3.0", "12.89", "12.71"), .Names = c("db_version", 
             "elapsed_time", "cpu_time"))

I would like to convert it to a data frame that looks like this:

testDF <- data.frame("db_version"="11.2.0.3.0","elapsed_time"=12.89,"cpu_time"=12.71)
head(testDF)
  db_version elapsed_time cpu_time
1 11.2.0.3.0        12.89    12.71

6 Answers 6

105

It's as simple as data.frame(as.list(testVect)). Or if you want sensible data types for your columns, data.frame(lapply(testVect, type.convert), stringsAsFactors=FALSE).

4
  • 2
    Oddly, the tibble analog of this does not work: data_frame(as.list(testVect)) return a 5 row data frame. Commented Mar 19, 2018 at 21:34
  • 8
    @Deleet tibble will work with as_tibble(as.list(testVect)) or as_data_frame(as.list(testVect)) (as_data_frame is an alias for as_tibble).
    – JWilliman
    Commented Apr 2, 2018 at 22:11
  • 2
    In line with comments by @Deleet and @JWillliman, data.table(as.list(...)) does not work, but instead as.data.table(as.list(...)) does.
    – merv
    Commented Jul 3, 2018 at 20:57
  • @Matthew Plourde Whether stringsAsFactors True or False, it gives the same data type. How to not change the data type?
    – MAZ
    Commented Jul 19, 2020 at 19:10
79

The answers from @MatthewPlourde and @JackRyan work, but if you have a long named vector it is annoying to have a data frame with one row and many columns. If you'd rather have a "key" column and a "value" column with many rows, any of the following should work:

data.frame(keyName=names(testVect), value=testVect, row.names=NULL)

##        keyName      value
## 1   db_version 11.2.0.3.0
## 2 elapsed_time      12.89
## 3     cpu_time      12.71


## Suggested by @JWilliman
tibble::enframe(testVect)

## # A tibble: 3 x 2
##   name         value
##   <chr>        <chr>
## 1 db_version   11.2.0.3.0
## 2 elapsed_time 12.89
## 3 cpu_time     12.71


## Suggested by @Joe
stack(testVect)
##       values          ind
## 1 11.2.0.3.0   db_version
## 2      12.89 elapsed_time
## 3      12.71     cpu_time
7
  • sad that there's no one-liner Commented Jan 31, 2018 at 16:29
  • 8
    Can also use tibble::enframe(testVect).
    – JWilliman
    Commented Apr 2, 2018 at 22:18
  • 4
    stack(testVect) also does this but leaves the values as characters.
    – Joe
    Commented Nov 10, 2018 at 19:15
  • @Jelena-bioinf as one-liner with dplyr syntax, you can use testVect %>% as.list %>% as.data.frame %>% tidyr::gather() This actually produces the 'key' and 'value' columns which @dnlbrky referred to.
    – Agile Bean
    Commented Nov 28, 2018 at 3:46
  • 2
    stack(), what an underrated function!
    – stevec
    Commented Jul 25, 2020 at 15:41
22

I'm going to take a stab at this:

test.vector <- as.data.frame(t(testVect))
class(test.vector)
1
  • 1
    Or even shorter, simply data.frame(t(testVect))
    – tjebo
    Commented Mar 4, 2019 at 18:06
10

I used to use the functions suggested in these answers (as.list, as_tibble, t, enframe, etc.) but have since found out that dplyr::bind_rows now works to do exactly what the original question asks with a single function call.

library(dplyr)
testVect <- structure(c("11.2.0.3.0", "12.89", "12.71"), .Names = c("db_version", "elapsed_time", "cpu_time"))
testVect %>% bind_rows
#> # A tibble: 1 x 3
#>   db_version elapsed_time cpu_time
#>   <chr>      <chr>        <chr>   
#> 1 11.2.0.3.0 12.89        12.71

Created on 2019-11-10 by the reprex package (v0.3.0)

As shown in tidyverse - prefered way to turn a named vector into a data.frame/tibble

4
named vector %>% as_tibble(.,rownames="column name of row.names")
1
  • Please add some explanation around here so everybody can learn. As of now a one-liner is a bit shallow. Commented Oct 31, 2020 at 18:22
2

Here's an example using tibble:

named_vector_df = tibble(name = names(named_vector), value = named_vector)

1
  • 1
    Note to novices like me: this creates a df with 2 columns, one named "name" (with names as data, not row.names) and one named "value". I understand that OP meant precisely this but I came here searching for another but similar problem and spent a long time trying to figure out why this method produced strange results...
    – Esmu Igors
    Commented Oct 13, 2021 at 21:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.