收集整齐的R重复的列名称

时间:2016-04-22 08:19:28

标签: r tidyr

我有一个类似的数据集

hojd <- structure(list(AVD = c(9L, 11L, 19L, 19L, 19L, 19L, 31L, 50L, 
61L, 69L, 75L, 2L, 3L, 5L, 6L, 8L, 10L, 11L, 11L, 13L, 13L, 14L, 
15L, 15L, 16L, 16L, 18L, 18L, 19L, 19L, 22L, 23L, 24L, 24L, 24L, 
26L, 27L, 27L, 30L, 31L, 31L, 31L, 34L, 34L, 35L, 38L, 38L, 38L, 
39L, 39L, 40L, 40L, 41L, 43L, 45L, 45L, 46L, 46L, 47L, 50L, 51L, 
53L, 57L, 59L, 59L, 60L, 60L, 64L, 65L, 65L, 66L, 68L, 68L, 69L, 
70L, 71L, 72L, 73L, 73L, 74L, 75L, 75L, 75L, 75L, 76L, 76L, 77L, 
79L, 81L), RAD = c(5L, 7L, 1L, 9L, 5L, 2L, 3L, 8L, 5L, 5L, 4L, 
2L, 3L, 3L, 5L, 4L, 4L, 9L, 1L, 9L, 2L, 2L, 2L, 5L, 1L, 7L, 3L, 
3L, 6L, 7L, 2L, 6L, 4L, 5L, 5L, 5L, 7L, 7L, 4L, 1L, 2L, 9L, 8L, 
5L, 2L, 8L, 8L, 1L, 4L, 8L, 6L, 5L, 7L, 3L, 4L, 1L, 5L, 6L, 8L, 
8L, 6L, 4L, 8L, 3L, 5L, 7L, 3L, 8L, 9L, 9L, 3L, 5L, 3L, 7L, 5L, 
2L, 4L, 1L, 7L, 2L, 2L, 9L, 1L, 3L, 2L, 4L, 2L, 4L, 4L), PL = c(5L, 
3L, 3L, 1L, 2L, 1L, 2L, 2L, 1L, 4L, 5L, 1L, 3L, 1L, 5L, 5L, 1L, 
1L, 4L, 3L, 4L, 5L, 3L, 1L, 2L, 5L, 2L, 3L, 3L, 2L, 1L, 3L, 3L, 
2L, 5L, 3L, 5L, 1L, 1L, 4L, 5L, 4L, 5L, 2L, 5L, 1L, 5L, 4L, 2L, 
5L, 2L, 5L, 5L, 5L, 2L, 2L, 4L, 2L, 5L, 1L, 5L, 5L, 2L, 1L, 2L, 
1L, 5L, 4L, 1L, 3L, 2L, 4L, 4L, 2L, 4L, 5L, 5L, 2L, 5L, 2L, 5L, 
1L, 1L, 1L, 5L, 1L, 1L, 2L, 2L), FLED = c(1L, 1L, 1L, 2L, 1L, 
1L, 1L, 2L, 1L, 2L, 2L, 9159L, 4750L, 0L, 8507L, 1438L, 9005L, 
1L, 2L, 4381L, 4632L, 1470L, 4986L, 8397L, 2411L, 1402L, 8590L, 
6727L, 2L, 2L, 413L, 4354L, 5534L, 269L, 131L, 427L, 8218L, 2467L, 
4820L, 1L, 1L, 2L, 0L, 144L, 7855L, 4110L, 4150L, 302L, 4243L, 
4586L, 131L, 157L, 5640L, 7635L, 3200L, 1528L, 2439L, 754L, 3930L, 
2L, 6798L, 1392L, 7863L, 6668L, 4381L, 2047L, 1977L, 999L, 30L, 
219L, 3569L, 2442L, 7398L, 2L, 2350L, 4354L, 5598L, 6909L, 5600L, 
2393L, 2L, 1L, 2L, 2L, 30L, 2542L, 131L, 4770L, 7905L), DIAM16 = c(129L, 
126L, 265L, 129L, 162L, 178L, 209L, 161L, 169L, 175L, 186L, 279L, 
181L, 279L, 301L, 299L, 203L, 299L, 231L, 202L, 107L, 284L, 141L, 
144L, 226L, 163L, 129L, 252L, 146L, 127L, 165L, 165L, 175L, 174L, 
272L, 268L, 303L, 228L, 251L, 268L, 179L, 94L, 140L, 203L, 145L, 
274L, 137L, 178L, 161L, 170L, 282L, 140L, 107L, 46L, 66L, 123L, 
159L, 172L, 216L, 125L, 207L, 262L, 227L, 275L, 108L, 181L, 214L, 
161L, 138L, 205L, 207L, 245L, 204L, 212L, 168L, 157L, 267L, 303L, 
285L, 188L, 257L, 281L, 158L, 195L, 315L, 171L, 298L, 148L, 152L
), H16 = c(135L, 141L, 207L, 158L, 186L, 205L, 194L, 183L, 177L, 
191L, 192L, 216L, 198L, 213L, 218L, 211L, 195L, 215L, 211L, 198L, 
NA, 213L, 195L, 180L, 215L, 194L, 127L, 184L, 173L, 174L, 188L, 
189L, 205L, 189L, 225L, 196L, NA, 186L, 211L, 206L, 199L, 140L, 
163L, 216L, 182L, 217L, 173L, 186L, 181L, 188L, 208L, 175L, 158L, 
NA, 102L, 156L, 182L, 190L, 194L, 159L, 192L, 193L, 200L, 213L, 
149L, 193L, 195L, 179L, 178L, 190L, 210L, 206L, 189L, 193L, 169L, 
160L, 176L, 217L, 201L, 204L, 201L, 228L, 172L, 206L, 206L, 207L, 
224L, 148L, 147L), x = c(106.4, 21, 0, 11.2, 5.6, 1.4, 40.6, 
60.2, 81.2, 68.6, 29.4, 14, 28, 53.2, 68.6, 92.4, 4.2, 23.8, 
12.6, 49, 39.2, 51.8, 64.4, 68.6, 75.6, 84, 103.6, 103.6, 7, 
8.4, 39.2, 57.4, 67.2, 68.6, 68.6, 93.8, 109.2, 109.2, 29.4, 
37.8, 39.2, 49, 85.4, 81.2, 89.6, 22.4, 22.4, 12.6, 29.4, 35, 
44.8, 43.4, 58.8, 78.4, 105, 100.8, 5.6, 7, 22.4, 60.2, 70, 92.4, 
35, 53.2, 56, 71.4, 65.8, 9.8, 23.8, 23.8, 28, 56, 53.2, 71.4, 
81.2, 89.6, 105, 0, 8.4, 14, 26.6, 36.4, 25.2, 28, 39.2, 42, 
51.8, 79.8, 105), y = c(5.6, 9.8, 16.8, 14, 15.4, 14, 22.4, 36.4, 
42, 53.2, 61.6, 0, 2.8, 0, 5.6, 5.6, 7, 7, 11.2, 9.8, 11.2, 12.6, 
9.8, 7, 8.4, 12.6, 8.4, 9.8, 16.8, 15.4, 14, 16.8, 16.8, 15.4, 
19.6, 16.8, 19.6, 14, 21, 25.2, 26.6, 25.2, 26.6, 22.4, 26.6, 
28, 33.6, 32.2, 29.4, 33.6, 29.4, 33.6, 33.6, 33.6, 29.4, 29.4, 
39.2, 36.4, 40.6, 35, 40.6, 40.6, 43.4, 42, 43.4, 42, 47.6, 53.2, 
49, 51.8, 50.4, 53.2, 53.2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 79L, 62L, 31L, 39L, 72L, 
64L, 72L, 81L, 79L, 79L, 102L, 82L, 70L, 53L, 67L, 91L, 56L, 
59L, 68L, 69L, 92L, 77L, 70L, 61L, 72L, 99L, 80L, 49L, 68L, 75L, 
81L, 95L, 73L, 71L, 57L, 74L, 51L, 69L), .Label = c("10", "100", 
"101", "102", "103", "104", "105", "106", "107", "108", "11", 
"110", "112", "12", "123", "13", "14", "15", "16", "17", "18", 
"19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", 
"3", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", 
"4", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", 
"5", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", 
"6", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", 
"70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "8", 
"80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "9", 
"90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "NA"
), class = "factor"), H94 = structure(c(57L, 44L, 61L, 44L, 47L, 
67L, 45L, 51L, 55L, 53L, 58L, 56L, 66L, 53L, 57L, 70L, 59L, 58L, 
70L, 50L, 56L, 54L, 51L, 51L, 60L, 54L, 47L, 69L, 58L, 45L, 49L, 
59L, 51L, 51L, 68L, 67L, 68L, 68L, 62L, 54L, 46L, 42L, 54L, 67L, 
58L, 55L, 55L, 44L, 37L, 56L, 60L, 46L, 37L, 28L, 33L, 54L, 43L, 
48L, 56L, 46L, 57L, 77L, 50L, 58L, 37L, 47L, 67L, 39L, 38L, 42L, 
62L, 65L, 51L, 53L, 42L, 55L, 60L, 44L, 34L, 49L, 53L, 56L, 59L, 
55L, 44L, 46L, 61L, 33L, 44L), .Label = c("0", "10", "11", "12", 
"13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", 
"24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", 
"35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", 
"46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", 
"57", "58", "59", "6", "60", "61", "62", "63", "64", "65", "66", 
"67", "68", "69", "7", "70", "71", "72", "73", "74", "75", "76", 
"77", "78", "79", "8", "81", "82", "83", "84", "85", "86", "89", 
"9", "NA"), class = "factor"), h_bl = c(4, 1, 1, 1, 1, 1, 2, 
3, 4, 3, 2, 1, 2, 3, 3, 4, 1, 1, 1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 
1, 1, 2, 3, 3, 3, 3, 4, 4, 4, 2, 2, 2, 2, 4, 4, 4, 1, 1, 1, 2, 
2, 2, 2, 3, 4, 4, 4, 1, 1, 1, 3, 3, 4, 2, 3, 3, 3, 3, 1, 1, 1, 
2, 3, 3, 3, 4, 4, 4, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 4, 4), type = c("1", 
"1", "1", "2", "1", "1", "1", "2", "1", "2", "2", "KLON", "KLON", 
"KLON", "KLON", "KLON", "KLON", "1", "2", "KLON", "KLON", "KLON", 
"KLON", "KLON", "KLON", "KLON", "KLON", "KLON", "2", "2", "KLON", 
"KLON", "KLON", "KLON", "KLON", "KLON", "KLON", "KLON", "KLON", 
"1", "1", "2", "KLON", "KLON", "KLON", "KLON", "KLON", "KLON", 
"KLON", "KLON", "KLON", "KLON", "KLON", "KLON", "KLON", "KLON", 
"KLON", "KLON", "KLON", "2", "KLON", "KLON", "KLON", "KLON", 
"KLON", "KLON", "KLON", "KLON", "KLON", "KLON", "KLON", "KLON", 
"KLON", "2", "KLON", "KLON", "KLON", "KLON", "KLON", "KLON", 
"2", "1", "2", "2", "KLON", "KLON", "KLON", "KLON", "KLON")), class = "data.frame", .Names = c("AVD", 
"RAD", "PL", "FLED", "DIAM16", "H16", "x", "y", "block1", "beh", 
"DIAM94", "H94", "h_bl", "type"), row.names = c(NA, -89L))

我想要做的是计算特定变量的平均值,然后将其融合为长格式并在ggplot中使用它 我是这样做的

hojd %>%  group_by(beh) %>% summarise(SI94 = si_spr(12, mean(as.numeric(H94)/10, na.rm = T)),
                                      SI16 = si_spr(34, mean(H16/10,  na.rm = T))) %>% 
  gather(beh, value = "SI") 

问题是我得到了变量beh的重复列名:

     beh   beh    SI
   (int) (chr) (dbl)
1      1  SI94 37.56
2      2  SI94 38.18
3      3  SI94 37.26
4      4  SI94 39.54
5      5  SI94 36.79
6      6  SI94 37.45
7      7  SI94 37.17
8      8  SI94 37.73
9      9  SI94 37.77
10     1  SI16 34.17
11     2  SI16 36.33
12     3  SI16 35.81
13     4  SI16 36.17
14     5  SI16 35.36
15     6  SI16 35.13
16     7  SI16 36.16
17     8  SI16 35.28
18     9  SI16 35.17

1 个答案:

答案 0 :(得分:2)

这是正确的用法。我简化了summarise,因为我不知道si_spr是什么。

hojd %>% 
  group_by(beh) %>% 
  summarise(SI94 = mean(as.numeric(H94), na.rm = T),
            SI16 = mean(H16, na.rm = T)) %>% 
  gather(type, SI, -beh)

   beh type     SI
1    1 SI94  52.12
2    2 SI94  55.30
3    3 SI94  50.64
4    4 SI94  62.80
5    5 SI94  48.33
6    6 SI94  51.57
7    7 SI94  50.17
8    8 SI94  53.00
9    9 SI94  53.21
10   1 SI16 173.00
11   2 SI16 200.00
12   3 SI16 193.33
13   4 SI16 198.00
14   5 SI16 187.67
15   6 SI16 184.86
16   7 SI16 197.83
17   8 SI16 186.67
18   9 SI16 185.25