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,240 points
21,818 views

1 answer to this question.

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,130 points

edited Apr 12, 2018 by nirvana

Related Questions In Data Analytics

0 votes
2 answers

how to convert a data frame into a list in R

Convert whole data frame into a list?? ...READ MORE

answered Sep 4, 2019 in Data Analytics by anonymous
• 33,030 points
931 views
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,790 points
7,151 views
+1 vote
2 answers

How to sort a data frame by columns in R?

You can use dplyr function arrange() like ...READ MORE

answered Aug 21, 2019 in Data Analytics by anonymous
• 33,030 points
1,401 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 17, 2018 in Data Analytics by DataKing99
• 8,240 points
1,609 views
+1 vote
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,720 points
1,229 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,720 points
983 views
0 votes
1 answer
+1 vote
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 14, 2018 in Data Analytics by Sahiti
• 6,370 points
6,271 views
0 votes
1 answer

.SD in data.table in R

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

answered Apr 13, 2018 in Data Analytics by nirvana
• 3,130 points
6,597 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,130 points
670 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP