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?
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.
Try
do.call(rbind.data.frame, lapply(lst, as.list))
If the number of columns are not the same, usebind_rows
i.e.purrr::map_df(lst, as.list)
– akrun
Aug 30 at 16:44