How to convert a list to data frame in R?

0 votes

I am new to R and I'm trying ti convert a list to data frame.

Consider a nested list of data. Length of the list is 145 and each item has a list of length of 30.

Is there any way to convert this structure into a data frame of 145 rows and 30 columns?

l <- replicate(
  145,
  list(sample(letters, 30)),
  simplify = FALSE
)
Apr 12, 2018 in Data Analytics by DataKing99
• 8,100 points
1,283 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

Let's assume your list of lists is called 'a':

data <- data.frame(matrix(unlist(a), nrow=145, byrow=T))

The above command will convert all character columns to factors. But, to avoid this add the stringsAsFactors parameters to this call.
Refer below:

data <- data.frame(matrix(unlist(a), nrow=145, byrow=T),stringsAsFactors=FALSE)

NOTE: You need to be careful here if all your data is not of same type.
When you pass the data through a matrix, all your data is coerced into a common type.
For Example: If you have one column of character data and one column of numeric data then the numeric data will be coerced to string by matrix() and both to factor by data.frame()

answered Apr 12, 2018 by nirvana
• 3,040 points

edited Apr 12, 2018 by nirvana

Related Questions In Data Analytics

0 votes
1 answer

How to convert tables to a data frame in R ?

> trial.table.df <- as.data.frame(trial.table) //assuming that trial.table ...READ MORE

answered Apr 20, 2018 in Data Analytics by zombie
• 3,690 points
17 views
0 votes
1 answer

How to sort a data frame by columns in R?

You can just use the order function ...READ MORE

answered Apr 10, 2018 in Data Analytics by darklord
• 6,140 points
50 views
0 votes
1 answer

How to filter a data frame with dplyr and tidy evaluation in R?

Requires the use of map_df to run each model, ...READ MORE

answered May 16, 2018 in Data Analytics by DataKing99
• 8,100 points
43 views
0 votes
1 answer

How do I convert data frame to csv file in R?

Hey @Ali, its very simple one line ...READ MORE

answered Nov 21, 2018 in Data Analytics by Maverick
• 10,000 points
21 views
0 votes
1 answer

How to convert a list of vectors with various length into a Data.Frame?

We can easily use this command as.data.frame(lapply(d1, "length< ...READ MORE

answered Apr 4, 2018 in Data Analytics by DeepCoder786
• 1,700 points
31 views
0 votes
1 answer

How to create a list of Data frames?

Basically all we have to do is ...READ MORE

answered Apr 9, 2018 in Data Analytics by DeepCoder786
• 1,700 points
32 views
0 votes
1 answer
0 votes
1 answer

How to convert a list of dataframes in to a single dataframe using R?

You can use the plyr function: data <- ...READ MORE

answered Apr 13, 2018 in Data Analytics by darklord
• 6,140 points
61 views
0 votes
1 answer

.SD in data.table in R

.SD stands for "Subset of Data.table". The ...READ MORE

answered Apr 12, 2018 in Data Analytics by nirvana
• 3,040 points
675 views
0 votes
1 answer

Is there any way to check for missing packages and install them in R?

There are 2 options: Either you can use ...READ MORE

answered Apr 17, 2018 in Data Analytics by nirvana
• 3,040 points
31 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.