How to convert a list consisting of vector of different lengths to a usable data frame in R?
try this:
word.list <- list(letters[1:4], letters[1:5], letters[1:2], letters[1:6])
n.obs <- sapply(word.list, length)
seq.max <- seq_len(max(n.obs))
mat <- t(sapply(word.list, "[", i = seq.max))
the trick is, that,
c(1:2)[1:4]
returns the vector + two NAs
List of different length character vectors into data frame
The easiest would be stri_list2matrix
library(stringi)
df <- as.data.frame(stri_list2matrix(folders, byrow = TRUE), stringsAsFactors=FALSE)
names(df) <- paste0("Level", seq_along(df))
df
# Level1 Level2 Level3
#1 Main <NA> <NA>
#2 Main Mid <NA>
#3 Main Mid Sub
But, this can also be solved with base R
m1 <- max(lengths(folders))
d1 <- as.data.frame(do.call(rbind, lapply(folders, `length<-`, m1)), stringsAsFactors= FALSE)
names(d1) <- paste0("Level", seq_along(d1))
The simplest way to convert a list with various length vectors to a data.frame in R
We can use
data.frame(lapply(aa, "length<-", max(lengths(aa))))
Or using tidyverse
library(dplyr)
library(tibble)
library(tidyr)
enframe(aa) %>%
unnest(value)
Convert list of named vectors of different length to tibble
Try stack
> stack(example)
values ind
1 1 a
2 2 a
3 3 a
4 2 b
5 3 b
6 3 c
7 4 c
8 5 c
9 6 c
Function to transform lists of different lengths into data frame utilizing recycling
The issue is you need to apply listExtend
to each vector of fullList
in the DF
function.
x <- c(1:7)
y <- c("a","b","c","d","e")
z <- c(TRUE,TRUE,FALSE,FALSE)
fullList <- list(x,y,z)
DF <- function(x) {
maxLength <- max(lengths(x))
newList <- lapply(x, function(l) rep(l, length.out = maxLength))
finalDF <- data.frame(newList)
return(finalDF)
}
outDF <- DF(fullList)
colnames(outDF) <- c('x', 'y', 'z')
outDF
#------
x y z
1 1 a TRUE
2 2 b TRUE
3 3 c FALSE
4 4 d FALSE
5 5 e TRUE
6 6 a TRUE
7 7 b FALSE
How do you convert large list with vectors of different lenght to dataframe?
You could do:
t(as.data.frame(lapply(lst, "length<-", max(lengths(lst)))))
# [,1] [,2] [,3] [,4] [,5]
#a 1 2 4 5 6
#c 7 8 9 NA NA
#c.1 10 11 NA NA NA
Or as @Andrew pointed out, you can do:
t(sapply(lst, "length<-", max(lengths(lst))))
# [,1] [,2] [,3] [,4] [,5]
#a 1 2 4 5 6
#c 7 8 9 NA NA
#c 10 11 NA NA NA
How to put elements of a list of vectors of different lengths into a data frame, with elements of vectors being separated as different columns
The solution below (1) converts each list element into a dataframe where t()
is used to get the element names to be column names in the dataframe, and (2) row-binds those dataframes together where data.table's fill
argument is helpful for the inconsistent vector lengths/names.
mylist <- list(
one = c(SW=0.49, GHS=0.46, GS=0.03),
two = c(GHS=0.07, GW = 0.03, LLW=0.03, GS=0.00),
six = c(GHS=0.41, SW=0.42, TD=0.09)
)
temp <- lapply(mylist, function(x) data.frame(t(x)))
data.table::rbindlist(temp, fill=TRUE)
output:
SW GHS GS GW LLW TD
1: 0.49 0.46 0.03 NA NA NA
2: NA 0.07 0.00 0.03 0.03 NA
3: 0.42 0.41 NA NA NA 0.09
Related Topics
How to Remove Rows With Any Zero Value
Remove Specific Characters from Column Names in R
Ggplot2 Stacked Bar Chart - Each Bar Being 100% and With Percenage Labels Inside Each Bar
Multiplying All Columns in Dataframe by Single Column
Calculate Max Value Across Multiple Columns by Multiple Groups
How to Add Row and Column to a Dataframe of Different Length
How to Sort a Data Frame by Alphabetic Order of a Character Variable in R
Creating a New Column Based on Unique Id With Values in R
How to Remove Na from a Factor Variable (And from a Ggplot Chart)
R - Test If a String Vector Contains Any Element of Another List
Rstudio Suddenly Stopped Showing Plots in the Plot Pane
To Find Most Frequently Occuring Element in Matrix in R
How to Specify the Size of a Graph in Ggplot2 Independent of Axis Labels
Removing Duplicate Combinations (Irrespective of Order)
Add a Common Legend For Combined Ggplots