简体   繁体   中英

Convert columns of data.frame from lists to vectors

I have a data.frame created from a list "y" using the code

map_dfr(y, ~as.data.frame(t(.x))) 

The format works for me, but each column of the dataframe is itself a list, rather than the vector I'd like it to be. What can I do to flatten out each list such that I preserve the structure of my dataframe, but change the format of each column?

EDIT: Here's a small bit of the data in its current format. What I'm looking to do is unlist each variable into a vector so it takes the normal data.frame format.

structure(list(member_id = list("A000055", "A000361", "A000367", 
"A000369", "A000210", "B001256", "B000013", "B001279", "B001269", 
"B001282"), name = list("Robert B. Aderholt", "Rodney Alexander", 
"Justin Amash", "Mark Amodei", "Robert E. Andrews", "Michele Bachmann", 
"Spencer Bachus", "Ron Barber", "Lou Barletta", "Andy Barr"), 
party = list("R", "R", "R", "R", "D", "R", "R", "D", "R", 
    "R"), state = list("AL", "LA", "MI", "NV", "NJ", "MN", 
    "AL", "AZ", "PA", "KY"), district = list("4", "5", "3", 
    "2", "1", "6", "6", "2", "11", "6"), cook_pvi = list(
    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 
    NULL), vote_position = list("No", "Yes", "No", "Yes", 
    "Yes", "No", "Yes", "Yes", "Yes", "Yes"), dw_nominate = list(
    0.361, 0.331, 0.649, 0.376, -0.297, 0.584, 0.387, -0.123, 
    0.277, 0.485), bill_num = c("S47", "S47", "S47", "S47", 
"S47", "S47", "S47", "S47", "S47", "S47"), bill_title = c("Violence Against Women Reauthorization Act of 2013", 
"Violence Against Women Reauthorization Act of 2013", "Violence Against Women Reauthorization Act of 2013", 
"Violence Against Women Reauthorization Act of 2013", "Violence Against Women Reauthorization Act of 2013", 
"Violence Against Women Reauthorization Act of 2013", "Violence Against Women Reauthorization Act of 2013", 
"Violence Against Women Reauthorization Act of 2013", "Violence Against Women Reauthorization Act of 2013", 
"Violence Against Women Reauthorization Act of 2013")), row.names = c(NA, 10L), class = "data.frame")

Probably, this is what you want :

y[] <- lapply(y, unlist)

This will convert the data into normal data.frame form.

str(y)
#'data.frame':  10 obs. of  9 variables:
# $ member_id    : chr  "A000055" "A000361" "A000367" "A000369" ...
# $ name         : chr  "Robert B. Aderholt" "Rodney Alexander" "Justin Amash" "Mark Amodei" ...
# $ party        : chr  "R" "R" "R" "R" ...
# $ state        : chr  "AL" "LA" "MI" "NV" ...
# $ district     : chr  "4" "5" "3" "2" ...
# $ vote_position: chr  "No" "Yes" "No" "Yes" ...
# $ dw_nominate  : num  0.361 0.331 0.649 0.376 -0.297 0.584 0.387 -0.123 0.277 0.485
#$ bill_num     : chr  "S47" "S47" "S47" "S47" ...
# $ bill_title   : chr  "Violence Against Women Reauthorization Act of 2013" "Violence Against Women Reauthorization Act of 2013" "Violence Against Women Reauthorization Act of 2013" "Violence Against Women Reauthorization Act of 2013" ...

If we have list of multiple lengths we can use unnest and pass a range of columns.

library(tidyr)
library(dplyr)

z <- y %>% 
       unnest(cols = member_id:district)  %>%
       type.convert(as.is = TRUE) %>%
       arrange(desc(district))

We can use arrange(district) to sort them in ascending order.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM