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

Updated on: 11-Aug-2020

76 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements