Convert Named Character Vector to data.frame

RDataframeVectorType Conversion

R Problem Overview


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

R Solutions


Solution 1 - R

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).

Solution 2 - R

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

Solution 3 - R

I'm going to take a stab at this:

test.vector <- as.data.frame(t(testVect))
class(test.vector)

Solution 4 - R

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 https://stackoverflow.com/questions/40036207/tidyverse-prefered-way-to-turn-a-named-vector-into-a-data-frame-tibble

Solution 5 - R

named vector %>% as_tibble(.,rownames="column name of row.names")

Solution 6 - R

Here's an example using tibble:

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTyler MuthView Question on Stackoverflow
Solution 1 - RMatthew PlourdeView Answer on Stackoverflow
Solution 2 - RdnlbrkyView Answer on Stackoverflow
Solution 3 - RJack RyanView Answer on Stackoverflow
Solution 4 - RArthur YipView Answer on Stackoverflow
Solution 5 - RBatmanFanView Answer on Stackoverflow
Solution 6 - ROliver OliverView Answer on Stackoverflow