Going from a list to a data frame keeping the column name

Going from a list to a data frame keeping the column name


[[29]]
type lineNum cat sel
"I" "25" "DOR" "MN-A"
act desc descChanged calc
"+" "Door labor minimum" "1" "1"
qty unit coverageName replace
"1" "EA" "Dwelling" "58.58"
total acv recoverable acvTotal
"58.58" "58.58" "1" "58.58"
rcvTotal isPartOfInitSettle laborTotal laborBase
"58.58" "0" "58.58" "58.58"

[[30]]
type lineNum cat sel
"I" "26" "WDW" "MN-A"
act desc descChanged calc
"+" "Window labor minimum" "1" "1"
qty unit coverageName replace
"1" "EA" "Dwelling" "69.5"
total acv recoverable acvTotal
"69.5" "69.5" "1" "69.5"
rcvTotal isPartOfInitSettle laborBase
"69.5" "0" "69.5"



Here is a sample of my data.The number of columns are not the same. I am trying to create a data frame from this. I essentially want two rows here and match up by the column name.



I tried using plyr but it doesn't work as the number of columns is not the same.



Any suggestion?





Try do.call(rbind.data.frame, lapply(lst, as.list)) If the number of columns are not the same, use bind_rows i.e. purrr::map_df(lst, as.list)
– akrun
Aug 30 at 16:44



do.call(rbind.data.frame, lapply(lst, as.list))


bind_rows


purrr::map_df(lst, as.list)





is you item a dataframe? or just vectors? if data.frames then plyr::rbind.fill(lst) will do the job
– Onyambu
Aug 30 at 16:58



plyr::rbind.fill(lst)




2 Answers
2



We could use map from purrr


map


purrr


library(dplyr)
library(purrr)
map_df(lst, as.list)
# A tibble: 3 x 5
# A B C D E
# <int> <int> <int> <int> <int>
#1 1 2 3 NA NA
#2 1 2 NA NA NA
#3 1 2 3 4 5


lst <- list(setNames(1:3, LETTERS[1:3]), setNames(1:2, LETTERS[1:2]),
setNames(1:5, LETTERS[1:5]))



I just figured out that


plyr::ldply(Item_list, rbind)



does the job



Required, but never shown



Required, but never shown






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)