I have a list I created through a process that looks like this (so that I could pull from tidycensus):
dv_acs = c(
hus = "B25002_001",
husocc = "B25002_002",
husvac = "B17001_002"
)
I want to know if I can turn that into a data.frame. When I try to ask it to be a data.frame by:
out <- data.frame(dv_acs)
Then I get this:
dv_acs
hus B25002_001
husocc B25002_002
husvac B17001_002
Where the lefthand names replace what is normally 1:n but is not, in itself, a column of variables (data is noted as 3 obs of 1 variable).
1) This uses only base R.
stack(dv_acs)
## values ind
## 1 B25002_001 hus
## 2 B25002_002 husocc
## 3 B17001_002 husvac
2) We can also use enframe
library(tibble)
enframe(dv_acs)
## # A tibble: 3 × 2
## name value
## <chr> <chr>
## 1 hus B25002_001
## 2 husocc B25002_002
## 3 husvac B17001_002
This seems to be a named character vector, not a list as understood in R. In any case, with the data you provided, one can create a data frame by doing:
data.frame(name = names(dv_acs), dv_acs = unname(dv_acs))
#> name dv_acs
#> 1 hus B25002_001
#> 2 husocc B25002_002
#> 3 husvac B17001_002
Or
tibble::rownames_to_column(as.data.frame(dv_acs), 'name')
#> name dv_acs
#> 1 hus B25002_001
#> 2 husocc B25002_002
#> 3 husvac B17001_002
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With