- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to create a data frame with one or more columns as a list in R?
Creating a data frame with a column as a list is not difficult but we need to use I with the list so that the list elements do not work as an individual column. Here, you will find the common method to create a list which is incorrect if we want to insert that list in our data, also the correct method is mentioned at the end.
The incorrect way −
Example
> x1<-1:20 > x2<-list(c(1,1),c(2,2),c(3,3),c(4,4),c(5,5),c(6,6),c(7,7),c(8,8),c(9,9), + c(10,10),c(11,11),c(12,12),c(13,13),c(14,14),c(15,15),c(16,16),c(17,17), + c(18,18),c(19,19),c(20,20)) > df<-data.frame(x1,x2) > df x1 c.1..1. c.2..2. c.3..3. c.4..4. c.5..5. c.6..6. c.7..7. c.8..8. c.9..9. 1 1 1 2 3 4 5 6 7 8 9 2 2 1 2 3 4 5 6 7 8 9 3 3 1 2 3 4 5 6 7 8 9 4 4 1 2 3 4 5 6 7 8 9 5 5 1 2 3 4 5 6 7 8 9 6 6 1 2 3 4 5 6 7 8 9 7 7 1 2 3 4 5 6 7 8 9 8 8 1 2 3 4 5 6 7 8 9 9 9 1 2 3 4 5 6 7 8 9 10 10 1 2 3 4 5 6 7 8 9 11 11 1 2 3 4 5 6 7 8 9 12 12 1 2 3 4 5 6 7 8 9 13 13 1 2 3 4 5 6 7 8 9 14 14 1 2 3 4 5 6 7 8 9 15 15 1 2 3 4 5 6 7 8 9 16 16 1 2 3 4 5 6 7 8 9 17 17 1 2 3 4 5 6 7 8 9 18 18 1 2 3 4 5 6 7 8 9 19 19 1 2 3 4 5 6 7 8 9 20 20 1 2 3 4 5 6 7 8 9 c.10..10. c.11..11. c.12..12. c.13..13. c.14..14. c.15..15. c.16..16. 1 10 11 12 13 14 15 16 2 10 11 12 13 14 15 16 3 10 11 12 13 14 15 16 4 10 11 12 13 14 15 16 5 10 11 12 13 14 15 16 6 10 11 12 13 14 15 16 7 10 11 12 13 14 15 16 8 10 11 12 13 14 15 16 9 10 11 12 13 14 15 16 10 10 11 12 13 14 15 16 11 10 11 12 13 14 15 16 12 10 11 12 13 14 15 16 13 10 11 12 13 14 15 16 14 10 11 12 13 14 15 16 15 10 11 12 13 14 15 16 16 10 11 12 13 14 15 16 17 10 11 12 13 14 15 16 18 10 11 12 13 14 15 16 19 10 11 12 13 14 15 16 20 10 11 12 13 14 15 16 c.17..17. c.18..18. c.19..19. c.20..20. 1 17 18 19 20 2 17 18 19 20 3 17 18 19 20 4 17 18 19 20 5 17 18 19 20 6 17 18 19 20 7 17 18 19 20 8 17 18 19 20 9 17 18 19 20 10 17 18 19 20 11 17 18 19 20 12 17 18 19 20 13 17 18 19 20 14 17 18 19 20 15 17 18 19 20 16 17 18 19 20 17 17 18 19 20 18 17 18 19 20 19 17 18 19 20 20 17 18 19 20
The correct way −
Example
> x1<-1:20 > x2<-I(list(c(1,1),c(2,2),c(3,3),c(4,4),c(5,5),c(6,6),c(7,7),c(8,8),c(9,9), + c(10,10),c(11,11),c(12,12),c(13,13),c(14,14),c(15,15),c(16,16),c(17,17), + c(18,18),c(19,19),c(20,20))) > df<-data.frame(x1,x2) > head(df,20) x1 x2 1 1 1, 1 2 2 2, 2 3 3 3, 3 4 4 4, 4 5 5 5, 5 6 6 6, 6 7 7 7, 7 8 8 8, 8 9 9 9, 9 10 10 10, 10 11 11 11, 11 12 12 12, 12 13 13 13, 13 14 14 14, 14 15 15 15, 15 16 16 16, 16 17 17 17, 17 18 18 18, 18 19 19 19, 19 20 20 20, 20
- Related Articles
- How to get list of all columns except one or more columns from an R data frame?
- Create a data frame with numerical as well as factor columns in R.
- How to create a data frame in R with list elements?
- How to extract data frame columns stored in a list in R?
- How to create scatterplot using data frame columns in R?
- How to create line chart for all columns of a data frame a in R?
- How to create a single data frame from data frames stored in a list with row names in R?
- How to convert a data frame with categorical columns to numeric in R?
- How to subset R data frame rows if at least one or more values matches?
- How to create a subset of an R data frame based on multiple columns?
- How to create histogram of all columns in an R data frame?
- How to subset one or more sub-elements of a list in R?
- How to create scatterplot by standardizing the columns of a data frame in base R?
- How to change a data frame with comma separated values in columns to multiple columns in R?
- How to convert a matrix to a data frame with column names and row names as new columns in R?

Advertisements