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 with vectors of different length into data frame
df = data.frame(col1 = unlist(lapply(ex.list, paste, collapse = ", ")))
> df
col1
1 Tom, Ron, Joe
2 Ron, Joe
3 Tom
To get the unique names you'd have to run something like this
> unique(unlist(strsplit(as.character(df[ , 1]), ", ")))
[1] "Tom" "Ron" "Joe"
You're not going to be able to look at the levels of the factor with the way you want your data.frame setup.
Convert a list to a data frame
Update July 2020:
The default for the parameter stringsAsFactors
is now default.stringsAsFactors()
which in turn yields FALSE
as its default.
Assuming your list of lists is called l
:
df <- data.frame(matrix(unlist(l), nrow=length(l), byrow=TRUE))
The above will convert all character columns to factors, to avoid this you can add a parameter to the data.frame() call:
df <- data.frame(matrix(unlist(l), nrow=132, byrow=TRUE),stringsAsFactors=FALSE)
Convert a list of vectors of different lengths to a data frame
expand.grid
should work in this case -
expand.grid(ll)
# f1 f2 f3
#1 a d f
#2 b d f
#3 c d f
#4 a e f
#5 b e f
#6 c e f
Another similar alternative would be purrr::cross_df
.
purrr::cross_df(ll)
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))
how to convert a list with different length of lists to a dataframe in r
We use mtabulate
and transpose the output
library(qdapTools)
t(mtabulate(data))
Or if we are using base R
, then stack
into a data.frame
with 2 columns and apply the table
table(stack(data))
Assuming that there are no duplicates for each entry. If there are duplicates, then we may need a logical vector coerced to binary
+(table(stack(data)) >0)
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
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
Related Topics
Ggplot X-Axis Labels with All X-Axis Values
Gantt Style Time Line Plot (In Base R)
How to Calculate the Probability for a Given Quantile in R
Round a Posix Date (Posixct) with Base R Functionality
In R, What Does a Negative Index Do
Heatmap-Like Plot, But for Categorical Variables
Plotting During a Loop in Rstudio
Generating All Permutations of N Balls in M Bins
Submit Form with No Submit Button in Rvest
Find Multiple Strings Using Str_Extract_All
Group Data and Plot Multiple Lines
Read Gzipped CSV Directly from a Url in R
Find Out the Number of Days of a Month in R
Downloading Png from Shiny (R)
Current Time in Iso 8601 Format
Bars in Geom_Bar Have Unwanted Different Widths When Using Facet_Wrap