R:如何用前两列作为自变量重塑数据框?

时间:2019-07-05 09:12:51

标签: r dataframe reshape2 dcast

我试图建立一个数据框,其中月份和站点为自变量,长度为唯一的因变量。但是我不确定哪个功能可以提供帮助。目前,我的数据帧较长,并且希望将其扩展。

我尝试使用dcast(程序包:reshape2),但是它将因变量汇总为length。因此,它不是告诉每个数据点作为输出,而是告诉我每个月和站点的组合都有多少个数据点。

structure(list(month = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 
4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 
4, 4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L, 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, 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), .Label = c("port", "bluff", "palme"), class = "factor"), 
    length = c(14.5, 8, 9.6, 11.3, 16.8, 13.4, 13.3, 16.1, 12.1, 
    9.5, 13.6, 9.5, 20.1, 19, 21.8, 14, 13.6, 16.4, 22, 15.6, 
    15.7, 25.4, 32, 30, 11.5, 18.3, 10.2, 7.4, 9, 12.5, 36.45, 
    51.5, 56, 35.1, 35.5, 31.1, 39.9, 55.4, 39.4, 72, 48, 15.7, 
    39.95, 21.3, 25.9, 42.2, 25.2, 60, 26.1, 44.2, 31.5, 41.3, 
    34.6, 46.7, 33, 52.2, 20.8, 16.9, 16, 55.3, 108.3, 84.4, 
    100, 32.95, 43, 132, 144.4, 101.2, 106.4, 87.7, 113.7, 25, 
    123, 126.8, 61.25, 94, 126, 98.8, 102.6, 107.6, 137, 98.7, 
    29.1, 136.9, 83.5, 32.1, 63.7, 95.5, 95.8, 117.4, 47.4, 54.2, 
    78.4, 98.5, 47.1, 55.3, 122.4, 34.5, 60.2, 64.5, 44.9, 71, 
    66.3, 33.6, 33.7, 61.7, 38.5, 44.9, 36.8, 15.4, 15.6, 21.3, 
    16.4, 63.6, 40, 111.1, 39.1, 65.7, 45.9, 20, 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, 14, 18.7, 13.5, 15.2, 16.5, 19.3, 6.5, 
    19.8, 17, 19.1, 14.5, 14.1, 10.1, 15.4, 14.3, 13.4, 4.8, 
    15.4, 17.5, 11.6, 8.4, 10.1, 11.4, 6.9, 13, 29, 30.7, 17.2, 
    10.7, 15.5, 45.5, 34.5, 23.5, 41, 32.1, 21.9, 52.1, 25.1, 
    19, 57.8, 46.5, 26.8, 64.5, 65, 68, 69.4, 13.2, 71.6, 67, 
    45.6, 22.7, 61.5, 27.8, 35, 40.9, 49.3, 27.9, 15.2, 27.4, 
    11.5, 55.8, 123, 144.2, 88.2, 48.3, 56.1, 159, 111.4, 170.5, 
    69, 77.6, 67.9, 69.8, 18.1, 96.8, 116.7, 124.5, 25.2, 120, 
    122.4, 95, 16, 117.1, 26.3, 124.3, 100.8, 36.9, 47, 16.6, 
    94.2, 54.3, 40.6, 138.7, 56, 87.2, 82.5, 53, 77.5, 63.5, 
    73.8, 43.1, 35.3, 139, 77.6, 77.9, 91, 48.2, 121.3, 43.4, 
    163, 144.9, 67.6, 62.9, 52.9, 85.1, 67.5, 56.6, 47.1, 153.7, 
    43.1, 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, 28.8, 72.3, 21, 58.7, 52.5, 34.7, 61, 29.1, 
    47.9, 60.1, 73.4, 38.8, 64.2, 60.4, 35.6, 53.6, 66.4, 75.1, 
    29.3, 39.5, 38.6, 41.7, 61.4, 79.5, 69.8, 77.7, 74.2, 26.6, 
    27, 62.9, 54.9, 56.5, 50, 36.2, 49.8, 46.4, 36, 38.3, 28.4, 
    96.2, 118.3, 133.4, 98.5, 140.4, 145, 144.9, 123.85, 59.2, 
    137.9, 137.1, 51.9, 115.1, 48.6, 123.5, 136.8, 148, 135.8, 
    15, 143.9, 83.4, 38.2, 25.4, 26.3, 82, 106.2, 83.3, 99.8, 
    102.9, 107.5, 71.6, 69.3, 68.5, 128.8, 143.3, 125.5, 172.9, 
    154.1, 141.9, 111.3, 78.2, 118.9, 168.1, 26.1, 160.6, 78.1, 
    74.8, 36.5, 152.4, 39.8, 116.1, 56.2, 51.3, 69.7, 34.9, 35.5, 
    31.2, 57, 49, 64, 54.2, 30.5, 47.2, 63, 65.3, 27.8, 26.5, 
    24.2, 32.9, 33.4, 33.6, 68.9, 70, 18.4, 33.2, 31.4, 23.5, 
    40.7, 21, 51.9, 23.3)), row.names = c(NA, -450L), class = "data.frame")

理想情况下,最终的数据帧将有一个月列,一个站点,每个月和站点组合30列(即使数据帧长到宽)。

enter image description here

Desireable output

1 个答案:

答案 0 :(得分:0)

您可以尝试tidyverse

library(tidyverse)
df %>% 
  as_tibble() %>% 
  group_by(month, site) %>% 
  mutate(index=1:n()) %>% 
  spread(index, length)
# A tibble: 15 x 32
# Groups:   month, site [15]
   month site    `1`   `2`   `3`   `4`   `5`   `6`   `7`   `8`   `9`  `10`  `11`  `12`  `13`  `14`  `15`  `16`  `17`  `18`
   <dbl> <fct> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
 1     1 port   14.5   8     9.6  11.3  16.8  13.4  13.3  16.1  12.1   9.5  13.6   9.5  20.1  19    21.8  14    13.6  16.4
 2     1 bluff  14    18.7  13.5  15.2  16.5  19.3   6.5  19.8  17    19.1  14.5  14.1  10.1  15.4  14.3  13.4   4.8  15.4
 3     1 palme   0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0  
 4     2 port   36.4  51.5  56    35.1  35.5  31.1  39.9  55.4  39.4  72    48    15.7  40.0  21.3  25.9  42.2  25.2  60  
 5     2 bluff  45.5  34.5  23.5  41    32.1  21.9  52.1  25.1  19    57.8  46.5  26.8  64.5  65    68    69.4  13.2  71.6
 6     2 palme  28.8  72.3  21    58.7  52.5  34.7  61    29.1  47.9  60.1  73.4  38.8  64.2  60.4  35.6  53.6  66.4  75.1
 7     3 port  108.   84.4 100    33.0  43   132   144.  101.  106.   87.7 114.   25   123   127.   61.2  94   126    98.8
 8     3 bluff  55.8 123   144.   88.2  48.3  56.1 159   111.  170.   69    77.6  67.9  69.8  18.1  96.8 117.  124.   25.2
 9     3 palme  54.9  56.5  50    36.2  49.8  46.4  36    38.3  28.4  96.2 118.  133.   98.5 140.  145   145.  124.   59.2
10     4 port   47.4  54.2  78.4  98.5  47.1  55.3 122.   34.5  60.2  64.5  44.9  71    66.3  33.6  33.7  61.7  38.5  44.9
11     4 bluff  54.3  40.6 139.   56    87.2  82.5  53    77.5  63.5  73.8  43.1  35.3 139    77.6  77.9  91    48.2 121. 
12     4 palme  38.2  25.4  26.3  82   106.   83.3  99.8 103.  108.   71.6  69.3  68.5 129.  143.  126.  173.  154.  142. 
13     5 port    0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0  
14     5 bluff   0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0     0  
15     5 palme  56.2  51.3  69.7  34.9  35.5  31.2  57    49    64    54.2  30.5  47.2  63    65.3  27.8  26.5  24.2  32.9
# ... with 12 more variables: `19` <dbl>, `20` <dbl>, `21` <dbl>, `22` <dbl>, `23` <dbl>, `24` <dbl>, `25` <dbl>, `26` <dbl>,
#   `27` <dbl>, `28` <dbl>, `29` <dbl>, `30` <dbl>