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,130 points
3,934 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,060 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 in Data Analytics by anonymous
• 32,260 points
56 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,710 points
114 views
0 votes
2 answers

How to sort a data frame by columns in R?

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

answered Aug 21 in Data Analytics by anonymous
• 32,260 points
163 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,130 points
148 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
135 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
113 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 13, 2018 in Data Analytics by darklord
• 6,190 points
215 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,060 points
1,848 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,060 points
57 views