Melt the data frame with 4 columns to three columns

倾然丶 夕夏残阳落幕 提交于 2019-12-24 04:26:09

问题


I have a data with 4 columns date, ville, borne and combined. I want to reshape this data to long format using reshape package.

Head of data :

> head(newdata)
                 date ville borne   comb
1 2010-01-01 00:00:00 46200 78500 124700
2 2010-01-01 01:00:00 46300 74100 120400
3 2010-01-01 02:00:00 46400 77600 124000
4 2010-01-01 03:00:00 46500 75600 122100
5 2010-01-01 04:00:00 46500 79000 125500
6 2010-01-01 05:00:00 46600 75500 122100

The reproducible data is as follows:

newdata <- structure(list(date = structure(list(sec = c(0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0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21L, 22L, 23L, 0L, 
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 
15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 0L, 1L, 2L, 3L, 
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 
18L, 19L, 20L, 21L, 22L, 23L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 
21L, 22L, 23L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 
12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 0L, 
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 
15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 0L, 1L, 2L, 3L, 
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 
18L, 19L, 20L, 21L, 22L, 23L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 
21L, 22L, 23L, 0L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 
12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 0L, 
1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 
15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 0L, 1L, 2L, 3L, 
4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 
18L, 19L, 20L, 21L, 22L, 23L), mday = c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1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0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), year = c(110L, 110L, 110L, 
110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 
110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 
110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 
110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 
110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 
110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 
110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 
110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 
110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 
110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 
110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 
110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 
110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 
110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 
110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 
110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 
110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 
110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 
110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 
110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 
110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 
110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 
110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 
110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 
110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 
110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 
110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 
110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 
110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 
110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 
110L, 110L, 110L), wday = c(5L, 5L, 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)), .Names = c("sec", "min", "hour", "mday", "mon", 
"year", "wday", "yday", "isdst"), class = c("POSIXlt", "POSIXt"
)), ville = c(46200, 46300, 46400, 46500, 46500, 46600, 46500, 
46600, 46600, 46500, 46500, 46500, 46400, 46400, 46300, 46300, 
40700, 40700, 40600, 40500, 40500, 40400, 40400, 40400, 40300, 
40300, 40200, 33800, 34300, 34600, 35000, 35200, 35300, 35500, 
35600, 38300, 38000, 37900, 37800, 37700, 37600, 37400, 37400, 
37200, 37100, 37000, 36900, 33000, 33300, 33400, 33500, 33600, 
33600, 33600, 33600, 33500, 33500, 33500, 33500, 33400, 34000, 
31600, 31600, 31600, 31700, 31700, 31600, 31600, 31500, 31400, 
31400, 31300, 31200, 31100, 31000, 32100, 32500, 32700, 32800, 
32800, 32900, 32900, 32900, 32900, 32800, 32900, 32900, 32900, 
32800, 32800, 32700, 32700, 32700, 32600, 32700, 32600, 32600, 
32600, 32600, 32600, 32500, 32500, 32400, 32400, 28900, 29000, 
29200, 29300, 29400, 30100, 31000, 30900, 30800, 30800, 30700, 
30600, 30600, 30600, 30400, 30400, 30300, 30300, 30300, 30200, 
30200, 30100, 30100, 30000, 30000, 30000, 29900, 29900, 30200, 
29900, 27100, 27200, 27300, 27300, 27400, 26000, 23000, 23200, 
23400, 23500, 23600, 25000, 25100, 27700, 27700, 27500, 27500, 
27400, 27400, 27300, 27200, 27300, 28500, 27200, 27200, 27200, 
27200, 27200, 27200, 27200, 27200, 27200, 26200, 26200, 26200, 
26200, 26300, 26200, 26200, 26200, 26200, 26100, 26100, 26100, 
26100, 26100, 26000, 26100, 26100, 26100, 26100, 26200, 26100, 
26200, 26200, 26300, 26300, 26300, 26300, 26300, 26300, 26300, 
26300, 26300, 26200, 26100, 26100, 26000, 25900, 25900, 25900, 
25800, 25800, 25700, 25700, 25600, 25200, 24100, 21400, 21500, 
21700, 21700, 21800, 21900, 21900, 22000, 21900, 22000, 22000, 
22100, 22100, 22100, 22100, 22100, 23200, 22100, 22100, 22000, 
22000, 22000, 21900, 21900, 21900, 21800, 21800, 21800, 21000, 
19400, 19500, 19500, 19500, 19500, 19600, 19600, 19600, 19600, 
19700, 19700, 19700, 19700, 19700, 19700, 19700, 19700, 19800, 
19800, 19900, 20000, 20000, 20100, 20900, 22400, 22400, 22300, 
22200, 22100, 22100, 22000, 21800, 21800, 21700, 20100, 21700, 
20100, 20100, 20100, 20100, 20000, 19900, 19900, 19800, 19700, 
19700, 19600, 15300, 15400, 15500, 15500, 15600, 15700, 15700, 
15700, 15800, 15900, 15900, 15900, 17600, 16000, 16000, 16000, 
16100, 16100, 16100, 16100, 16100, 16100, 16200, 16100, 16200, 
16100, 16200, 16200, 16100, 16200, 16200, 16200, 16200, 16200, 
16200, 16300, 16300, 16400, 16400, 16500, 16500, 16500, 16500, 
16500, 16500, 16400, 16500, 16500), borne = c(78500, 74100, 77600, 
75600, 79000, 75500, 76600, 72300, 75700, 75400, 75700, 78700, 
76900, 76500, 72800, 75100, 74700, 80200, 75200, 74900, 74700, 
73600, 69900, 73600, 70600, 74100, 75800, 73100, 71400, 72300, 
71300, 72400, 72700, 72200, 69400, 72600, 68900, 67700, 66000, 
64800, 66700, 68400, 65500, 66600, 63600, 106000, 106000, 109000, 
110000, 110000, 110000, 110000, 114000, 112000, 112000, 111000, 
110000, 109000, 108000, 108000, 106000, 105000, 110000, 113000, 
113000, 112000, 111000, 110000, 93500, 62600, 62700, 63300, 63300, 
63300, 63300, 63000, 63200, 62900, 62600, 62900, 62500, 62400, 
62900, 62800, 62200, 62500, 62200, 62100, 62200, 62100, 59300, 
60000, 60000, 60100, 60500, 60700, 60800, 60700, 60900, 61100, 
60800, 61000, 60900, 60800, 60300, 60500, 60600, 59300, 59500, 
59400, 59800, 60000, 59300, 56200, 56800, 56700, 56600, 56100, 
55700, 54400, 54100, 54100, 54000, 53800, 53700, 52800, 52400, 
52200, 52300, 52400, 48400, 48800, 49100, 49400, 49800, 49800, 
49700, 49800, 50200, 47100, 47400, 47500, 47700, 47500, 47600, 
47700, 47500, 47300, 47300, 47300, 47200, 47300, 46900, 45500, 
45700, 45900, 45600, 45700, 46000, 45800, 45700, 45900, 46000, 
45800, 45900, 45900, 46100, 46000, 45900, 46000, 45900, 43600, 
43700, 43700, 43600, 43700, 43500, 43500, 43500, 43200, 43300, 
43200, 41400, 41500, 41800, 41900, 41700, 41700, 41900, 41900, 
41900, 41900, 41700, 41800, 41800, 41800, 41800, 41800, 41700, 
41800, 41700, 41700, 41600, 41700, 41800, 41600, 40700, 40700, 
40800, 37800, 38100, 38600, 38800, 38700, 38700, 38400, 38200, 
38200, 38100, 37900, 37700, 37600, 37400, 37300, 37200, 37000, 
36900, 36800, 36600, 33200, 30400, 30900, 31300, 31500, 31700, 
31900, 32000, 32100, 32300, 32300, 32400, 32500, 32500, 32600, 
32700, 32800, 32800, 32900, 33000, 33000, 33100, 33200, 33300, 
33300, 33400, 33600, 33600, 33800, 33900, 34000, 34000, 34100, 
30900, 30900, 31000, 30900, 30800, 30700, 30600, 30600, 30500, 
30400, 30400, 30300, 30200, 30300, 30300, 30200, 30100, 30100, 
30000, 29900, 29900, 29900, 29800, 29700, 29700, 29700, 29700, 
29600, 29500, 29400, 29400, 29400, 29400, 29500, 29400, 29400, 
29300, 29200, 29200, 29300, 29100, 29100, 29200, 29100, 29100, 
29100, 29100, 29000, 29000, 29000, 29000, 29000, 28900, 28900, 
28900, 28900, 28900, 28800, 28900, 28800, 28800, 28800, 28700, 
28700, 28700, 28700, 28700, 28600, 28600, 28600, 28500, 28500, 
28400, 30700), comb = c(124700, 120400, 124000, 122100, 125500, 
122100, 123100, 118900, 122300, 121900, 122200, 125200, 123300, 
122900, 119100, 121400, 115400, 120900, 115800, 115400, 115200, 
114000, 110300, 114000, 110900, 114400, 116000, 106900, 105700, 
106900, 106300, 107600, 108000, 107700, 105000, 110900, 106900, 
105600, 103800, 102500, 104300, 105800, 102900, 103800, 100700, 
143000, 142900, 142000, 143300, 143400, 143500, 143600, 147600, 
145600, 145600, 144500, 143500, 142500, 141500, 141400, 140000, 
136600, 141600, 144600, 144700, 143700, 142600, 141600, 125000, 
94000, 94100, 94600, 94500, 94400, 94300, 95100, 95700, 95600, 
95400, 95700, 95400, 95300, 95800, 95700, 95000, 95400, 95100, 
95000, 95000, 94900, 92000, 92700, 92700, 92700, 93200, 93300, 
93400, 93300, 93500, 93700, 93300, 93500, 93300, 93200, 89200, 
89500, 89800, 88600, 88900, 89500, 90800, 90900, 90100, 87000, 
87500, 87300, 87200, 86700, 86100, 84800, 84400, 84400, 84300, 
84000, 83900, 82900, 82500, 82200, 82300, 82400, 78300, 78700, 
79300, 79300, 76900, 77000, 77000, 77100, 77600, 73100, 70400, 
70700, 71100, 71000, 71200, 72700, 72600, 75000, 75000, 74800, 
74700, 74700, 74300, 72800, 72900, 73200, 74100, 72900, 73200, 
73000, 72900, 73100, 73200, 73000, 73100, 73100, 72300, 72200, 
72100, 72200, 72200, 69800, 69900, 69900, 69800, 69800, 69600, 
69600, 69600, 69300, 69300, 69300, 67500, 67600, 67900, 68100, 
67800, 67900, 68100, 68200, 68200, 68200, 68000, 68100, 68100, 
68100, 68100, 68100, 67900, 67900, 67800, 67700, 67500, 67600, 
67700, 67400, 66500, 66400, 66500, 63400, 63300, 62700, 60200, 
60200, 60400, 60100, 60000, 60100, 60000, 59900, 59600, 59600, 
59400, 59400, 59300, 59100, 59000, 58900, 59800, 55300, 52500, 
52900, 53300, 53500, 53600, 53800, 53900, 53900, 54100, 54100, 
53400, 51900, 52000, 52100, 52200, 52300, 52400, 52500, 52600, 
52600, 52800, 52900, 53000, 53000, 53100, 53300, 53300, 53500, 
53700, 53800, 53900, 54100, 50900, 51000, 51900, 53300, 53200, 
53000, 52800, 52700, 52600, 52400, 52200, 52100, 51900, 50400, 
52000, 50300, 50200, 50200, 50100, 49900, 49800, 49800, 49600, 
49400, 49400, 49300, 45000, 45000, 45000, 44900, 45000, 45100, 
45100, 45200, 45200, 45300, 45200, 45100, 46800, 45300, 45100, 
45100, 45300, 45200, 45200, 45200, 45200, 45100, 45200, 45100, 
45200, 45100, 45100, 45100, 45000, 45100, 45100, 45000, 45100, 
45000, 45000, 45100, 45000, 45100, 45100, 45200, 45200, 45100, 
45100, 45100, 45000, 44900, 44900, 47200)), .Names = c("date", 
"ville", "borne", "comb"), row.names = c(NA, 336L), class = "data.frame")

I used the following formula to reshape the data:

renewdata <- melt(newdata,id=c("date"))

but I got the following error:

Error in data.frame(ids, variable, value, stringsAsFactors = FALSE) : 
  arguments imply differing number of rows: 336, 1008

However, if I use the above formula with only two data columns, then it works fine. Does melt function work with only two columns ? Any suggestions to solve this problem is highly appreciated.

The desired output is as follows:

                 date    variable value
1 2010-01-01 00:00:00     ville 46200
2 2010-01-01 01:00:00     ville 46300
3 2010-01-01 02:00:00     ville 46400
4 2010-01-01 03:00:00     ville 46500
5 2010-01-01 04:00:00     ville 46500
6 2010-01-01 05:00:00     ville 46600

回答1:


I just tried it using reshape2 package and I get the same error. The problem seems to be recycling. That is, columns of class POSIXlt POSIXt don't seem to recycle.

Let me explain in detail now. First type reshape2:::melt.data.frame to have a look at this function. If you type now:

debugonce(reshape2:::melt.data.frame)
melt(newdata, id.var = "date")

Then, you'll be in debug mode. Keep hitting enter until you see this output:

debug: df <- data.frame(ids, variable, value, stringsAsFactors = FALSE)
Browse[2]> 

Before you hit enter, check what ids looks like. It's a data.frame with one column that's exactly the same as your first column (date). That is it's dimensions are 336*1. Now, if you hit enter one more time, you'll see the error message you posted:

Error in data.frame(ids, variable, value, stringsAsFactors = FALSE) : 
  arguments imply differing number of rows: 336, 1008

So, what's happening here? The error comes from the line:

df <- data.frame(ids, variable, value, stringsAsFactors = FALSE)

Here, because ids is a data.frame whose column is of type POSIXlt POSIXt, the recycling doesn't seem to happen. That is, if you do:

x <- data.frame(date = 1:4) 
data.frame(x, y = 1:20)

you see that the date column gets recycled and this works fine! Now, let's try with x as a POSIXlt POSIXt object.

x <- data.frame(date = newdata$date[1:4]) 
# Note: the above assignment will convert x$date to POSIXct 
# (as POSIXlt is internally a list)
x$date <- as.POSIXlt(x$date) # get x$date to POSIXlt
data.frame(x, y = 1:20) # the way reshape2 handles melt function
Error in data.frame(x, y = 1:20) : 
  arguments imply differing number of rows: 4, 20

So, basically, POSIXlt POSIXt is not recycled. You can convert the date column to POSIXct for example and then try it. For ex, this works:

x <- data.frame(date = newdata$date[1:4])
data.frame(x, y = 1:20)
# works fine!

Hope this helps.



来源:https://stackoverflow.com/questions/16971937/melt-the-data-frame-with-4-columns-to-three-columns

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!