Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting list to dataframe in R

Tags:

list

dataframe

r

So I have a list, say:

L1 <- list(1:10, 5:14, 10:19)

Now I am trying to get the output of the list as dataframe such that my output looks:

 1. 1  2  3  4  5  6  7  8  9 10
 2. 5  6  7  8  9 10 11 12 13 14
 3. 10 11 12 13 14 15 16 17 18 19

I am using

as.data.frame(L1, row.names = TRUE)

and

list_vect2df(L1)

But none of them are giving the required output

like image 839
Ethan Avatar asked Mar 31 '18 18:03

Ethan


People also ask

Can you convert a list to a DataFrame in R?

data. frame() can be used to convert a list to R DataFrame or create a data frame from a list. If you want the elements in the list column-wise, then use cbind otherwise you can use rbind. Also learned to use different third-party packages to create DataFrame from a list.

How do I convert data into a DataFrame in R?

frame() function in R Programming Language is used to convert an object to data frame. These objects can be Vectors, Lists, Matrices, and Factors. Parameters: object: Vector, Matrix, factor, or data frame.

How do I convert a list to a numeric DataFrame in R?

To convert R List to Numeric value, use the combination of the unlist() function and as. numeric() function. The unlist() function in R produces a vector that contains all the atomic components.

How to convert a large list to a Dataframe in R?

In this article, we will discuss how to convert a large list to a dataframe in the R Programming Language. First, create a large list. Then use the Map function on the list and convert it to dataframe using the as.data.frame function in R.

Can you convert a list to a data frame?

However, because there are things, you can do with a dataframe that you cannot do with a list, it is helpful to be able to convert from one to the other to get the added flexibility. Why Convert A List To A Data Frame? When dealing with data structures, it is often necessary to switch between different types.

How to generate vectors from a list in R?

Data <- list (A = seq ( 1, 4 ), B = c ( "A", "D", "G", "L" ), C = c ( "Best", "List", "Dataframe", "Rstats" )) We can also display our list (and the vectors) like this: In the two code chunks above, we used the list () function together with two functions to first create the list called Data. Here we used c and seq in R to generate vectors.

How to create a Dataframe from a list in Python?

When you create a dataframe from a list or nested list you have to change the structure of the list into that of a dataframe. However, the elements of the list need to match to avoid producing errors when creating the resulting data frame. As you can see the as.data-xpx.frame () function has produced a dataframe from the list ...


1 Answers

You can unlist and use matrix, then converting to data.frame. It seems to be faster for this case.

as.data.frame(matrix(unlist(L1),nrow=length(L1),byrow=TRUE)

microbenchmark::microbenchmark(
a= map_dfr(L1, ~as.data.frame(t(.x))),
b= do.call(rbind, lapply(L1, function(x) as.data.frame(t(x)))),
c= as.data.frame(t(as.data.frame(L1))),
d= data.table::transpose(as.data.frame(L1)),
e= as.data.frame(matrix(unlist(L1),nrow=length(L1),byrow=TRUE)),
times = 100,unit = "relative")

# Unit: relative
# expr       min        lq      mean    median        uq       max neval
# a  9.146545  8.548656  8.859087  8.859051  9.449237  7.265274   100
# b 13.879833 11.523000 11.433790 10.924726 10.797251 24.012107   100
# c 12.719835 10.635809 10.442108 10.229913 10.259789  7.020377   100
# d 10.439881  9.143530  9.205734  8.859026  9.176125  6.624454   100
# e  1.000000  1.000000  1.000000  1.000000  1.000000  1.000000   100
like image 86
Moody_Mudskipper Avatar answered Oct 02 '22 11:10

Moody_Mudskipper