reshape2::melt counter function (unstack)

Asked

Viewed 222 times

2

I’m working with data.frame and it is organized in long format. However I would like to put in wide format according to a variable (FAT2) so that the provisions of the columns would remain: AVA, FAT1, Banana, Ingá, Gliricídia, Pupunha.

However, I would not like to convert to matrix, and then again to data.frame. The function melt package reshape2 does the opposite of what I need, because it stacks the variables.

dice:

dados<-structure(list(AVA = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 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6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L), .Label = c("Março de 2016", 
"Agosto de 2016", "Dezembro de 2016", "Março de 2017", "Agosto de 2017", 
"Fevereiro de 2018", "Abril de 2018", "Agosto de 2018"), class = c("ordered", 
"factor")), FAT1 = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
2L, 2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banana", "Gliricídia", "Ingá", "Pupunha", 
    "Solteiro"), class = "factor"), LUX = c(39018.33, 38870, 
    40375, 39686.67, 53363.33, 55666.67, 56888.33, 57768.33, 
    5029.33, 4758, 4810.67, 5044.67, 17840.67, 2917.33, 8508.67, 
    9960.67, 3014, 4704.67, 5712, 3815.33, 4683.33, 49213.33, 
    54353.33, 57226.67, 13939.33, 13869.33, 5100.67, 15864, 1809.47, 
    1814.53, 6084.67, 2357.33, 28333.33, 37586.67, 35640, 36833.33, 
    55886.67, 59886.67, 63193.33, 63346.67, 25313.33, 36593.33, 
    24466.67, 38420, 26700, 29106.67, 36746.67, 52300, 25586.67, 
    13106.67, 2782.67, 15677.33, 18310.67, 2864.67, 1893.13, 
    2330, 5256.67, 5313.33, 3916, 5219.33, 5176.67, 6183.33, 
    2959.33, 3823.33, 803.93, 1815.87, 1629.93, 1886.47, 7705.33, 
    2350, 2322.6, 1715.8, 25313.33, 23700, 38420, 28253.33, 26700, 
    11562.67, 49326.67, 52300, 9258, 7078, 6374, 7147, 6435, 
    8366, 9639, 129220, 3481, 1973.8, 4097, 3584, 4189, 1573, 
    3312, 2488, 2886, 2908, 4489, 4047, 4641, 3429, 3434, 4903, 
    4341, 4352, 5019, 8046, 4060, 4552, 5445, 7159, 7870, 4004, 
    5660, 9790, 8772, 6728, 129010, 128520, 27106.67, 19360, 
    28766.67, 25513.33, 29606.67, 30206.67, 21666.67, 34660, 
    15920, 18108.67, 6322, 2402.67, 19686.67, 28853.33, 2898, 
    3403.33, 2437.33, 4086, 25520, 22993.33, 2664, 4850, 3688, 
    3084.67, 22528.67, 4182, 24286.67, 4442.67, 29561.54, 29606.67, 
    28740, 34120, 16304.67, 12944.67, 25466.67, 25513.33, 26006.67, 
    30836.36, 35130, 34645.45, 15926.67, 16664, 24580, 15746, 
    15780.67, 37533.33, 63600, 21560, 12336, 15016.67, 8820.67, 9112, 29880, 35580, 31173.33, 21893.33, 5828, 8477.33, 8122.67, 
    13715.33, 7430, 14023.33, 13144.67, 6759.33, 5126.67, 7038.67, 
    13430.67, 13701.33, 8657.33, 14273.33, 21368, 18332, 16500, 
    16000, 14870.67, 14990.67, 13547.33, 14310, 14806.67, 13180, 
    39786.67, 58420, 56646.67, 59280, 60213.33, 60633.33, 61533.33, 
    64240, 46886.67, 55386.67, 9316, 43553.33, 5883.33, 5913.33, 
    39906.67, 13561.33, 29660, 11585.33, 25340, 8721.33, 57513.33, 
    58613.33, 5214, 60060, 9409.33, 36626.67, 22033.33, 7980.67, 
    7192, 5508, 57680, 9765.33, 39020, 35806.67, 56393.33, 50346.67, 
    23554, 54246.67, 63540, 62333.33, 14585, 54200, 55500, 18350, 
    18340, 54100, 58200, 12260, 17285, 15520, 6565, 5650, 34116.67, 
    4145, 27601.33, 3215, 2110, 2425, 4955, 5000, 2955, 3230, 
    9165, 4550, 3310, 6505, 4375, 4635, 4260, 3635, 4205, 3480, 
    10820, 12205, 16245, 13600, 17000, 13425.33, 11295, 11950, 
    4350, 5040, 6060, 16640, 10440, 24000, 29100, 30700, 3280, 
    3150, 2810, 2735, 17050, 20650, 9645, 10050, 4775, 4370, 
    5575, 5340, 3490, 6555, 5060, 5015, 2375, 2320, 4015, 3265, 
    7570, 6380, 28550, 26750, 4100, 3110, 14715, 15260, 4915, 
    4740, 27850, 17625, 3015, 3760, 4460, 4720, 5115, 5655, 6030, 
    9560, 2015, 2070, 2015, 2135, 2320, 2050, 2895, 2855, 2085, 
    2355, 2160, 2000, 2600, 2955, 3050, 3020, 2010, 2030, 2990, 
    2890, 3785, 3880, 4610, 3890, 2165, 2835, 4625, 4650, 4045, 
    4190, 6715, 6340, 4640, 5425, 8295, 16705, 28450, 17340, 
    11920, 16360, 4455, 4690, 4580, 4485, 11455, 10970, 13070, 
    11050, 3080, 3650, 3425, 3225, 9400, 9245, 9250, 7560, 4015, 
    3930, 14690, 15655, 24650, 25050, 10755, 9665, 4560, 2800, 
    6540, 15120, 23650, 24500, 11780, 11835, 14585, 54200, 55500, 
    18350, 18340, 54100, 58200, 12260, 17285, 15520, 6565, 5650, 
    38184, 35550, 19769.33, 10050, 14730, 43700, 7765, 53400, 
    3490, 6555, 9165, 4550, 3310, 6505, 4375, 4635, 4260, 3635, 
    4205, 3480, 10820, 12205, 16245, 13600, 16700, 16920, 11295, 
    11940, 6378.67, 8702, 12030.67, 12763.33, 11382.67, 10196.67, 
    15526.67, 25286.67, 6464.67, 6798.67, 7080.67, 7154, 7233.33, 
    7295.33, 8962, 8216.67, 5050, 5228, 5123.33, 5759.33, 4839.33, 
    5850, 10330.67, 9663.33, 6067.33, 5602.67, 11512, 11443.33, 
    8872, 6894.67, 15792, 17444, 6363.33, 6348, 10490, 10608.67, 
    10612, 10328.67, 20473.33, 21013.33, 12346.67, 11642, 11633.33, 
    10872.67, 11450, 14792.67, 15706.67, 16808.67, 5212, 6328.67, 
    7770.67, 7900.67, 6811.33, 8420.67, 9960, 11131.33, 4928.67, 
    5762, 3576.67, 5667.33, 4864, 8104, 8059.33, 10923.33, 8298.67, 
    7838.67, 7096.67, 7933.33, 8718, 8661.33, 10809.33, 13119.33, 
    11730, 11612.67, 8514.67, 10682, 11146.67, 11176, 16385.33, 
    15394.67, 5042, 5016, 4355.33, 10662.67, 8518.67, 9772, 9260.67, 
    14453.33, 2844, 3033.33, 3482.67, 3040.67, 8392, 8357.33, 
    6963.33, 7312.67, 2083.33, 2644.67, 3127.33, 3185.33, 5400, 
    6658.67, 6665.33, 7681.33, 3954.67, 3862.67, 5732, 6284.67, 
    6444.67, 6130.67, 9072.67, 10302.67, 4456, 4186.67, 9228.67, 
    8806.67, 7652.67, 7651.33, 11231.33, 11864.67, 10765.33, 
    10205.33, 13295.33, 181760, 25786.67, 28353.33, 28133.33, 
    32020, 6081.33, 6643.33, 5420.67, 6404, 9358.67, 9443.33, 
    14206.67, 5926.67, 5401.33, 5755.33, 3053.33, 3208.67, 3636.67, 
    8914, 5722, 2652, 8018, 7822, 8363.33, 6810, 14776, 8667.33, 
    29380, 12555, 10355.33, 8673.33, 13400.67, 13861.33, 18545.33, 
    5779.33, 28780, 31800, 10460.67, 14330.67, 16945.33, 14323.33, 
    12078.67, 17226.67, 19428.67, 25380, 9596.67, 10008, 10206.67, 
    9514.67, 8998.67, 9454, 12915.33, 13706.67, 5633.33, 6972.67, 
    6780, 9155.33, 9674, 13180.67, 10200, 9502, 10344.67, 10446, 
    14296.67, 14274.67, 12012.67, 11167.33, 24953.33, 24980, 
    10092, 9101.33, 12308.67, 13586, 11862, 12034, 22446.67, 
    25173.33)), row.names = c(NA, -640L), class = "data.frame")
  • 2

    Take a look at the function reshape2::dcast.

2 answers

3

First, you need to create an identifier variable:

dados$id <- 1:nrow(dados)

Then, the variables you want to stay in the row need to be on the left side of the formula, and in the right column. Finally, you need to tell which function you want to aggregate the data. In this case, I put sum, which will make no difference, will only keep the original data (as you have only 1 value) for each combination:

reshape2::dcast(dados, id + AVA + FAT1 ~ FAT2, fun.aggregate = sum, value.var = "LUX")
     id               AVA  FAT1   Banana Gliricídia     Ingá   Pupunha  Solteiro
1     1     Março de 2016 Antes     0.00       0.00     0.00      0.00  39018.33
2     2     Março de 2016 Antes     0.00       0.00     0.00      0.00  38870.00
3     3     Março de 2016 Antes     0.00       0.00     0.00      0.00  40375.00
4     4     Março de 2016 Antes     0.00       0.00     0.00      0.00  39686.67
5     5     Março de 2016 Antes     0.00       0.00     0.00      0.00  53363.33
6     6     Março de 2016 Antes     0.00       0.00     0.00      0.00  55666.67
7     7     Março de 2016 Antes     0.00       0.00     0.00      0.00  56888.33
8     8     Março de 2016 Antes     0.00       0.00     0.00      0.00  57768.33
9     9     Março de 2016 Antes     0.00       0.00  5029.33      0.00      0.00
10   10     Março de 2016 Antes     0.00       0.00  4758.00      0.00      0.00
11   11     Março de 2016 Antes     0.00       0.00  4810.67      0.00      0.00
12   12     Março de 2016 Antes     0.00       0.00  5044.67      0.00      0.00
13   13     Março de 2016 Antes     0.00       0.00 17840.67      0.00      0.00
14   14     Março de 2016 Antes     0.00       0.00  2917.33      0.00      0.00
15   15     Março de 2016 Antes     0.00       0.00  8508.67      0.00      0.00
16   16     Março de 2016 Antes     0.00       0.00  9960.67      0.00      0.00

However, if you want to aggregate the data, just:

reshape2::dcast(dados[, -5], AVA + FAT1 ~ FAT2, fun.aggregate = sum, value.var = "LUX")

                 AVA  FAT1    Banana Gliricídia      Ingá   Pupunha  Solteiro
1      Março de 2016 Antes 182722.66   60839.33  58870.01 380706.67 381636.66
2      Março de 2016  Após  37847.99   20229.93  82551.81 255576.00 269646.67
3     Agosto de 2016 Antes  30737.00   42974.00  24697.80 300354.00 183517.00
4     Agosto de 2016  Após  69323.33  177468.22  97594.67 206847.82 216886.68
5   Dezembro de 2016 Antes  77500.66  101928.00 163812.00 118205.34 211390.67
6   Dezembro de 2016  Após 256707.32  156195.33 220407.33 385240.67 460753.33
7      Março de 2017 Antes  34390.00   34405.00 114098.00 106540.33 285535.00
8      Março de 2017  Após  40180.00   81225.00  69370.00  92315.00 126330.00
9     Agosto de 2017 Antes  20225.00   26085.00  18355.00  35565.00  42315.00
10    Agosto de 2017  Após  48835.00  108410.00  64755.00 100785.00 109135.00
11 Fevereiro de 2018 Antes 143355.00   34405.00 148573.33 109725.00 285535.00
12 Fevereiro de 2018  Após  51843.99   83628.00  59205.34  96237.33 102267.35
13     Abril de 2018 Antes  51885.33   72475.33  63535.34  96642.01 105252.68
14     Abril de 2018  Após  37445.99   51784.69  43426.00  65078.01  67080.67
15    Agosto de 2018 Antes  38343.33   96391.66  63484.67 131195.32 330319.32
16    Agosto de 2018  Após  71098.00  122475.34  84400.68 116604.00 130174.01

there yes it returns the data.frame with the sum of each of the species of FAT2 for the value of LUX.

3


Using the , you first need to create a by aggregating the values of LUX and then spread it (spread()):

library(tidyverse)

dados %>% 
  group_by(AVA, FAT1, FAT2) %>% 
  summarise(LUX = sum(LUX)) %>% 
  spread(FAT2, LUX)

# Groups:   AVA, FAT1 [16]
   AVA               FAT1   Banana Gliricídia    Ingá Pupunha Solteiro
   <ord>             <fct>   <dbl>      <dbl>   <dbl>   <dbl>    <dbl>
 1 Março de 2016     Antes 182723.     60839.  58870. 380707.  381637.
 2 Março de 2016     Após   37848.     20230.  82552. 255576   269647.
 3 Agosto de 2016    Antes  30737      42974   24698. 300354   183517 
 4 Agosto de 2016    Após   69323.    177468.  97595. 206848.  216887.
 5 Dezembro de 2016  Antes  77501.    101928  163812  118205.  211391.
 6 Dezembro de 2016  Após  256707.    156195. 220407. 385241.  460753.
 7 Março de 2017     Antes  34390      34405  114098  106540.  285535 
 8 Março de 2017     Após   40180      81225   69370   92315   126330 
 9 Agosto de 2017    Antes  20225      26085   18355   35565    42315 
10 Agosto de 2017    Após   48835     108410   64755  100785   109135 
11 Fevereiro de 2018 Antes 143355      34405  148573. 109725   285535 
12 Fevereiro de 2018 Após   51844.     83628   59205.  96237.  102267.
13 Abril de 2018     Antes  51885.     72475.  63535.  96642.  105253.
14 Abril de 2018     Após   37446.     51785.  43426   65078.   67081.
15 Agosto de 2018    Antes  38343.     96392.  63485. 131195.  330319.
16 Agosto de 2018    Após   71098     122475.  84401. 116604   130174.

The intermediate table only needed to be created because there was more than one row for each set of AVA + FAT1. If the information were already unique for each group, just use the spread().

  • What an elegant solution. I liked it more than the solution you use reshape2::dcast.

  • Thanks for the tip Tomás Barcellos and Marcus Nunes!!!

  • am using base %>% pivot_longer(cols=all_of(c("FAT1","FAT2")),names_to = "variable",values_to = "value") %>% group_by(FAT1, FAT2) , and I’m finding it difficult to use summarise(soma = sum(value)), Parenting it does not add up, if use mean(), sd() It’s okay, now it’s on sum(), He just repeats the values. Someone can tell me why?

  • I could not understand. I recommend asking another question and ensure the reproducibility of the example/problem.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.