将不同长度的嵌套列表放入另一个数据框中 [英] Put nested lists of different lengths into a dataframe one bellow another

查看:183
本文介绍了将不同长度的嵌套列表放入另一个数据框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎样才能把这个列表列表放在一个数据框中,在一个for循环中?也就是说,如何绕过错误:

pre $ 错误(函数(...,row.names = NULL,check .rows = FALSE,check.names =
TRUE,:参数意味着不同的行数:50,30,20

代码:

  ListA = list(firstA = 1:50,secondA = 1:50,thirdA = 1:50)
ListB = list(firstB = 1:30,secondB = 1:30,thirdB = 1:30)
ListC = list(firstC = 1:20,secondC = 1:20 ,thirdC = 1:20)

NestedList = list(ListA,ListB,ListC)

DataToWrite = list()

for(i in 1:length(NestedList)){
DataToWrite = c(DataToWrite,NestedList [i])
}
#在下一步我得到错误
df = as.data .frame(DataToWrite)

我想要在for循环中添加每个列表前一个列的结尾,在一个数据框中。我想要的样子如下图所示:
st我想获得的结论



我不在乎列名,我想保留第一个列表的名字。请注意列表中的列名称对于每个列表都是不同的,而且在合并到data.frame中时也会出现问题。 解决方案



  do.call(rbind,lapply(NestedList,function(x)as.data.frame x,col.names = c(First,Second,Third))))



<

 第一个第二个第三个
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
21 21 21 21
22 22 22 22
23 23 23 23
24 24 24 24
25 25 25 25
26 26 26 26
27 27 27 27
28 28 28 28
29 29 29 29
30 30 30 30
31 31 31 31
32 32 32 32
33 33 33 33
34 34 34 34
35 35 35 35
36 36 36 36
37 37 37 37
38 38 38 38
39 39 39 39
40 40 40 40
41 41 41 41
42 42 42 42
43 43 43 43
44 44 44 44
45 45 45 45
46 46 46 46
47 47 47 47
48 48 48 48
49 49 49 49
50 50 50 50
51 1 1 1
52 2 2 2
53 3 3 3
54 4 4 4
55 5 5 5
56 6 6 6
57 7 7 7
58 8 8 8
59 9 9 9
60 10 10 10
61 11 11 11
62 12 12 12
63 13 13 13
64 14 14 14
65 15 15 15
66 16 16 16
67 17 17 17
68 18 18 18
69 19 19 19
70 20 20 20
71 21 21 21
72 22 22 22
73 23 23 23
74 24 24 24
75 25 25 25
76 26 26 26
77 27 27 27
78 28 28 28
79 29 29 29
80 30 30 30
81 1 1 1
82 2 2 2
83 3 3 3
84 4 4 4
85 5 5 5
86 6 6 6
87 7 7 7
88 8 8 8
89 9 9 9
90 10 10 10
91 11 11 11
92 12 12 12
93 13 13 13
94 14 14 14
95 15 15 15
96 16 16 16
97 17 17 17
98 18 18 18
99 19 19 19
100 20 20 20






lapply 遍历列表,将每个元素(即每个嵌套列表)转换为一个数据框,并赋予这些列相同的名称。然后使用 rbind 将这些数据框绑定在一起。


How can i put this lists of lists together in a dataframe, inside a for loop? That is, how do I bypass the error:

 Error in (function (..., row.names = NULL, check.rows = FALSE, check.names =  
 TRUE, : arguments imply differing number of rows: 50, 30, 20

Code:

ListA=list(firstA=1:50, secondA=1:50,thirdA=1:50)
ListB=list(firstB=1:30, secondB=1:30, thirdB=1:30)
ListC=list(firstC=1:20, secondC=1:20, thirdC=1:20)

NestedList=list(ListA,ListB,ListC)

DataToWrite=list()

for (i in 1:length(NestedList)){
    DataToWrite=c(DataToWrite,NestedList[i]) 
    }
#at the next step i get the error 
df=as.data.frame(DataToWrite)

What i want is to add each list in the for loop to in the same columns of the previous one, at the end of the previous list, in a dataframe. What i want looks like in the picture: example of structure that I would like to obtain

I do not care about the column names, I would like to keep the names of the first list. Note that the list "columns" names will be different for each list and that may also present a problem when merging to data.frame.

解决方案

Like this?

do.call(rbind, lapply(NestedList, function(x)as.data.frame(x, col.names=c("First", "Second", "Third"))))

gives,

    First Second Third
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
21     21     21    21
22     22     22    22
23     23     23    23
24     24     24    24
25     25     25    25
26     26     26    26
27     27     27    27
28     28     28    28
29     29     29    29
30     30     30    30
31     31     31    31
32     32     32    32
33     33     33    33
34     34     34    34
35     35     35    35
36     36     36    36
37     37     37    37
38     38     38    38
39     39     39    39
40     40     40    40
41     41     41    41
42     42     42    42
43     43     43    43
44     44     44    44
45     45     45    45
46     46     46    46
47     47     47    47
48     48     48    48
49     49     49    49
50     50     50    50
51      1      1     1
52      2      2     2
53      3      3     3
54      4      4     4
55      5      5     5
56      6      6     6
57      7      7     7
58      8      8     8
59      9      9     9
60     10     10    10
61     11     11    11
62     12     12    12
63     13     13    13
64     14     14    14
65     15     15    15
66     16     16    16
67     17     17    17
68     18     18    18
69     19     19    19
70     20     20    20
71     21     21    21
72     22     22    22
73     23     23    23
74     24     24    24
75     25     25    25
76     26     26    26
77     27     27    27
78     28     28    28
79     29     29    29
80     30     30    30
81      1      1     1
82      2      2     2
83      3      3     3
84      4      4     4
85      5      5     5
86      6      6     6
87      7      7     7
88      8      8     8
89      9      9     9
90     10     10    10
91     11     11    11
92     12     12    12
93     13     13    13
94     14     14    14
95     15     15    15
96     16     16    16
97     17     17    17
98     18     18    18
99     19     19    19
100    20     20    20


So, lapply goes through the list converting each element (i.e., each nested list) to a data frame and giving the columns the same names. do.call then binds these data frames together using rbind.

这篇关于将不同长度的嵌套列表放入另一个数据框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆