Arules包:将连续向量离散为具有特定类别的离散向量,以产生R中的频率表

问题描述 投票:0回答:1

概观

我有一个名为“Canopy_Index”的连续向量和另一个名为“Species”的向量,这是一个因素。

我的目标是使用下面的R代码将Canopy_Index向量离散化为包含特定百分比类别的离散向量,间隔为10,即0-10,10-20,20-30,30-40,50-60,60-70 ,70-80,80-90,90-100。

目的是通过以下R-code example at the bottom of the page显示两种栎树(Quercus petraea和Quercus robur)的冠层指数的频率分布。

理想情况下,我想生成一个包含3列和11行的表。每行代表百分比类别,总共有10个类别。

列标题:

  1. 第1列= Canopy_Index的离散百分比类别
  2. 第2列= Q. Petraea的Canopy_Index类别的频率
  3. 第3列= Q. robur的Canopy_Index类别的频率

但是,我继续在下面生成错误消息。

如果有人能够提供帮助,我将非常感激。

R-代码

尝试1

Canopy_Interval1<-table(discretize(Discrete_Canopy, methods=list(
                                                                Canopy_Index=list(method="frequency", breaks=10,
                                                       labels=c("0-10", "10-20","20-30", "30-40", "50-60", "60-70", "70-80",
                                                                "80-90", "90-100"),
                                                                 Species=list(method="frequency", breaks=2,
                                                       labels=c("Quercus petraea", "Quercus robur"),
                                                       default = list(method = "none"))))))

 **Error message**

    Error in (1 - h) * qs[i] : non-numeric argument to binary operator

尝试2

  breaks <- seq(from = 0, to = 100, by = 10)
        labels <- map_chr(breaks[1:10], ~ paste(.x, .x + 10, sep="-"))

        #########Compartmentalise the number of tree species per canopy interval

        indices <- SpeciesLatitudeCanopy$Canopy_Index
        table(indices)


        ########Place the percentages into table form

        percents <- discretize(indices,
                               method = "fixed",
                               breaks = breaks,
                               labels = labels)

        ###Produce the table of percents
        table(percents)

        ##Produce the data table with binned data
               SpecLatCan<-SpeciesLatitudeCanopy %>%
                                   mutate(Canopy_Index_Binned = percents) 

        ##Structure
               head(SpecLatCan)

        ###Produce a data frame

               SpeciesLatitudeCanopy1<-data.frame(SpecLatCan)

        ###Check the structure of the data frame

               str(SpeciesLatitudeCanopy1)

        ##Count Canopy/Species

               SpeciesLatitudeCanopy1_Count<-count(SpeciesLatitudeCanopy1, c(Canopy_Index_Binned, Species))

        ##Error Message

Error in count(SpeciesLatitudeCanopy1, c(Canopy_Index_Binned, Species)) : 
  object 'Canopy_Index_Binned' not found

数据框的结构

enter image description here

数据框的负责人

enter image description here

数据框

structure(list(Date_observed = structure(c(10L, 15L, 3L, 3L, 
3L, 3L, 13L, 13L, 11L, 11L, 11L, 11L, 9L, 9L, 9L, 9L, 12L, 12L, 
12L, 12L, 6L, 6L, 10L, 10L, 10L, 10L, 14L, 14L, 14L, 14L, 13L, 
13L, 13L, 9L, 9L, 9L, 9L, 14L, 14L, 14L, 14L, 8L, 8L, 8L, 8L, 
14L, 7L, 7L, 7L, 7L, 12L, 12L, 12L, 12L, 12L, 12L, 5L, 5L, 5L, 
5L, 13L, 13L, 13L, 13L, 14L, 14L, 14L, 14L, 13L, 13L, 6L, 8L, 
8L, 8L, 1L, 1L, 1L, 1L, 6L, 6L, 6L, 6L, 13L, 13L, 13L, 13L, 12L, 
12L, 12L, 12L, 2L, 2L, 2L, 2L, 11L, 11L, 11L, 3L, 3L, 3L, 3L, 
14L, 14L, 14L, 9L, 9L, 9L, 9L, 14L, 14L, 13L, 13L, 13L, 13L, 
3L, 3L, 13L, 13L, 13L, 13L, 14L, 14L, 14L, 14L, 3L, 3L, 3L, 3L, 
14L, 14L, 14L, 14L, 11L, 11L, 11L, 11L, 13L, 13L, 13L, 13L, 3L, 
3L, 3L, 3L, 14L, 14L, 6L, 6L, 6L, 12L, 12L, 12L, 12L, 14L, 13L, 
13L, 13L, 13L, 14L, 14L, 14L, 14L, 11L, 11L, 11L, 14L, 14L, 14L, 
10L, 10L, 10L, 10L, 6L, 5L, 5L, 5L, 10L, 10L, 10L, 4L, 4L, 13L, 
13L, 13L, 13L, 9L, 9L, 9L, 13L, 13L, 13L, 14L, 14L, 14L, 14L, 
6L, 6L, 6L, 6L, 13L, 13L, 13L, 13L, 12L, 12L, 12L, 12L, 14L, 
14L, 14L, 14L, 6L, 6L, 6L, 6L), .Label = c("10/23/18", "11/18/18", 
"11/30/18", "11/6/18", "12/1/18", "12/10/18", "12/12/18", "12/2/18", 
"12/3/18", "12/4/18", "12/6/18", "12/7/18", "12/8/18", "12/9/18", 
"9/10/18"), class = "factor"), Latitude = c(51.41752, 52.243806, 
52.947709, 52.947709, 52.947709, 52.947709, 51.491811, 51.491811, 
51.60157, 51.60157, 51.60157, 51.60157, 52.68959, 52.68959, 52.68959, 
52.68959, 50.697802, 50.697802, 50.697802, 50.697802, 53.62417, 
53.62417, 50.446841, 50.446841, 50.446841, 50.446841, 53.959679, 
53.959679, 53.959679, 53.959679, 52.01434, 52.01434, 52.01434, 
51.78375, 51.78375, 51.78375, 51.78375, 51.456965, 51.456965, 
51.456965, 51.456965, 52.011812, 52.011812, 52.011812, 52.011812, 
55.91924, 50.121978, 50.121978, 50.121978, 50.121978, 51.43474, 
51.43474, 51.10708, 51.10708, 51.10708, 51.10708, 50.435984, 
50.435984, 50.435984, 50.435984, 51.78666, 51.78666, 51.78666, 
51.78666, 53.38728, 53.38728, 53.38728, 53.38728, 52.441088, 
52.441088, 52.552344, 53.582285, 53.582285, 53.582285, 49.259471, 
49.259471, 49.259471, 49.259471, 50.462, 50.462, 50.462, 50.462, 
51.746642, 51.746642, 51.746642, 51.746642, 52.2501, 52.2501, 
52.2501, 52.2501, 52.42646, 52.42646, 52.42646, 52.42646, 50.79387, 
50.79387, 50.79387, 53.615575, 53.615575, 53.615575, 53.615575, 
51.08478, 51.08478, 51.08478, 53.19329, 53.19329, 53.19329, 53.19329, 
55.968437, 55.968437, 56.52664, 56.52664, 56.52664, 56.52664, 
52.04252, 52.04252, 51.8113, 51.8113, 51.8113, 51.8113, 50.52008, 
50.52008, 50.52008, 50.52008, 51.48417, 51.48417, 51.48417, 51.48417, 
54.58243, 54.58243, 54.58243, 54.58243, 52.58839, 52.58839, 52.58839, 
52.58839, 52.717283, 52.717283, 52.717283, 52.717283, 50.740764, 
50.740764, 50.740764, 50.740764, 50.733412, 50.733412, 50.79926, 
50.79926, 50.79926, 53.675788, 53.675788, 53.675788, 53.675788, 
55.43828, 48.35079, 48.35079, 48.35079, 48.35079, 51.36445, 51.36445, 
51.36445, 51.36445, 52.36286, 52.36286, 52.36286, 52.122402, 
52.122402, 52.122402, 52.16104, 52.16104, 52.16104, 52.16104, 
54.7311, 51.61842, 51.61842, 51.61842, 55.920966, 55.920966, 
55.920966, 57.158724, 57.158724, 51.88468, 51.88468, 51.88468, 
51.88468, 52.34015, 52.34015, 52.34015, 53.37687, 53.37687, 53.37687, 
54.27745, 54.27745, 54.27745, 54.27745, 52.026042, 52.026042, 
52.026042, 52.026042, 51.319032, 51.319032, 51.319032, 51.319032, 
51.51365, 51.51365, 51.51365, 51.51365, 53.43202, 53.43202, 53.43202, 
53.43202, 51.50797, 51.50797, 51.50797, 51.50797), Longitude = c(-0.32116, 
1.30786, -1.435407, -1.435407, -1.435407, -1.435407, -3.210324, 
-3.210324, -3.67111, -3.67111, -3.67111, -3.67111, -3.3081, -3.3081, 
-3.3081, -3.3081, -2.11692, -2.11692, -2.11692, -2.11692, -2.43155, 
-2.43155, -3.706923, -3.706923, -3.706923, -3.706923, -1.061008, 
-1.061008, -1.061008, -1.061008, 1.04007, 1.04007, 1.04007, -0.65046, 
-0.65046, -0.65046, -0.65046, -2.624917, -2.624917, -2.624917, 
-2.624917, -0.70082, -0.70082, -0.70082, -0.70082, -3.20936, 
-5.555169, -5.555169, -5.555169, -5.555169, 0.45981, 0.45981, 
-2.32027, -2.32027, -2.32027, -2.32027, -4.105617, -4.105617, 
-4.105617, -4.105617, -0.71433, -0.71433, -0.71433, -0.71433, 
-2.95811, -2.95811, -2.95811, -2.95811, -0.176158, -0.176158, 
-1.337177, -2.802239, -2.802239, -2.802239, -123.107788, -123.107788, 
-123.107788, -123.107788, -3.5607, -3.5607, -3.5607, -3.5607, 
0.486416, 0.486416, 0.486416, 0.486416, -0.8825, -0.8825, -0.8825, 
-0.8825, -1.78771, -1.78771, -1.78771, -1.78771, 0.26684, 0.26684, 
0.26684, -2.432959, -2.432959, -2.432959, -2.432959, -0.73626, 
-0.73626, -0.73626, -0.63793, -0.63793, -0.63793, -0.63793, -3.179732, 
-3.179732, -3.40313, -3.40313, -3.40313, -3.40313, -2.43733, 
-2.43733, -0.22894, -0.22894, -0.22894, -0.22894, -4.20756, -4.20756, 
-4.20756, -4.20756, -0.34854, -0.34854, -0.34854, -0.34854, -5.93229, 
-5.93229, -5.93229, -5.93229, -1.96843, -1.96843, -1.96843, -1.96843, 
-2.410575, -2.410575, -2.410575, -2.410575, -2.361234, -2.361234, 
-2.361234, -2.361234, -2.014029, -2.014029, -3.19446, -3.19446, 
-3.19446, -1.272404, -1.272404, -1.272404, -1.272404, -4.64226, 
10.91812, 10.91812, 10.91812, 10.91812, -0.23106, -0.23106, -0.23106, 
-0.23106, -2.06327, -2.06327, -2.06327, -0.487443, -0.487443, 
-0.487443, 0.18702, 0.18702, 0.18702, 0.18702, -5.8041, -0.16034, 
-0.16034, -0.16034, -3.193503, -3.193503, -3.193503, -2.166099, 
-2.166099, -0.17853, -0.17853, -0.17853, -0.17853, -1.27795, 
-1.27795, -1.27795, -1.34506, -1.34506, -1.34506, -0.47911, -0.47911, 
-0.47911, -0.47911, -0.503113, -0.503113, -0.503113, -0.503113, 
-0.472994, -0.472994, -0.472994, -0.472994, -3.18722, -3.18722, 
-3.18722, -3.18722, -2.27968, -2.27968, -2.27968, -2.27968, -0.25931, 
-0.25931, -0.25931, -0.25931), Altitude = c(0, 0, 103.9, 103.9, 
103.9, 103.9, 15, 15, 184, 184, 184, 184, 176, 176, 176, 176, 
12, 12, 12, 12, 178, 178, 36, 36, 36, 36, 11, 11, 11, 11, 47, 
47, 47, 210, 210, 210, 210, 97, 97, 97, 97, 0, 0, 0, 0, 100, 
68, 68, 68, 68, 4, 4, 200, 200, 200, 200, 160, 160, 160, 160, 
165.8, 165.8, 165.8, 165.8, 0, 0, 0, 0, 0, 0, 0, 36, 36, 36, 
47, 47, 47, 47, 0, 0, 0, 0, 43, 43, 43, 43, 97, 97, 97, 97, 133, 
133, 133, 133, 18, 18, 18, 123, 123, 123, 123, 127, 127, 127, 
15, 15, 15, 15, 14, 14, 65, 65, 65, 65, 45, 45, 129, 129, 129, 
129, 18, 18, 18, 18, 30, 30, 30, 30, 19, 19, 19, 19, 0, 0, 0, 
0, 0, 0, 0, 0, 96, 96, 96, 96, 0, 0, 0, 0, 0, 49, 49, 49, 49, 
0, 0, 0, 0, 0, 48, 48, 48, 48, 123, 123, 123, 43, 43, 43, 75, 
75, 75, 75, 0, 73, 73, 73, 115, 115, 115, 119, 119, 94, 94, 94, 
94, 112, 112, 112, 34, 34, 34, 0, 0, 0, 0, 103, 103, 103, 103, 
0, 0, 0, 0, 37.5, 37.5, 37.5, 37.5, 29, 29, 29, 29, 63, 63, 63, 
63), Species = structure(c(2L, 2L, 2L, 2L, 1L, 1L, 2L, 2L, 2L, 
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 
1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 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, 1L, 2L, 1L, 2L, 
1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 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), .Label = c("Quercus petraea", 
"Quercus robur"), class = "factor"), Tree_diameter = c(68.8, 
300, 847, 817, 569, 892, 62, 71, 29, 46.5, 27.7, 40.1, 68, 45, 
60, 54, 104, 122, 85, 71, 81, 118, 39.8, 43.6, 44.6, 22.6, 19.8, 
16.6, 15.1, 11.9, 48.1, 83.7, 77.9, 81.8, 102.5, 75.5, 57.3, 
0.3, 0.2, 0.3, 0.3, 99, 85, 74, 68, 60, 67.3, 36.9, 82, 51.8, 
19.9, 14.6, 196, 122, 118, 180, 58.6, 54.1, 58, 61.5, 58.4, 40.6, 
61, 68.6, 117, 240, 210, 310, 134, 64, 52.2, 57, 73.9, 37.1, 
170, 114, 127, 158, 147.4, 135.3, 122.9, 104.1, 263, 237, 322, 
302, 173, 186, 144, 155, 89, 41, 68, 83, 81.5, 29.3, 43.3, 141.6, 
85.5, 82.8, 114.1, 129, 127, 143, 125, 92, 68, 90, 25, 20, 63.7, 
39.8, 66.2, 112.4, 41.9, 43.8, 124.5, 94.1, 68.6, 74.4, 24.2, 
54.7, 43, 33.1, 306, 274, 56, 60, 72.5, 128.5, 22, 16, 143, 103, 
53, 130, 48.4, 69.8, 6.4, 18.6, 129.2, 41.7, 57.6, 14, 320, 352, 
120.9, 108.3, 53.2, 240, 274, 122, 85, 21, 52, 43, 38, 37, 219, 
215, 216, 175, 124, 133, 119, 85.9, 49.7, 97.1, 40.8, 79.3, 62.4, 
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15L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 45L, 45L, 55L, 
35L, 35L, 55L, 35L, 5L, 5L, 5L, 5L, 95L, 95L, 95L, 95L, 15L, 
35L, 45L, 25L, 25L, 15L, 5L, 25L, 25L, 25L, 25L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 35L, 25L, 5L, 35L, 15L, 15L, 
35L, 35L, 25L, 25L, 5L, 5L, 5L, 5L, 35L, 25L, 25L, 25L, 5L, 5L, 
15L, 15L, 35L, 65L, 35L, 35L, 25L, 25L, 5L, 25L, 25L, 25L, 25L, 
15L, 15L, 5L, 35L, 35L, 45L, 35L, 5L, 15L, 15L, 25L, 5L, 15L, 
5L, 5L, 15L, 5L, 5L, 15L, 5L, 5L, 5L, 5L, 5L, 85L, 5L, 35L, 15L, 
5L, 5L, 5L, 25L, 25L, 15L, 35L, 95L, 95L, 95L, 95L, 15L, 15L, 
5L, 25L, 25L, 5L, 15L, 15L, 5L, 5L, 15L, 5L, 5L, 5L, 25L, 25L, 
25L, 25L, 5L, 5L, 5L, 5L, 15L, 25L, 15L, 25L, 25L, 55L, 35L, 
35L, 25L, 25L, 45L, 5L, 25L, 5L, 5L, 5L, 5L, 55L, 55L, 15L, 15L, 
25L, 15L, 45L, 35L, 35L, 15L, 5L, 25L, 15L, 15L, 15L, 15L, 15L, 
35L, 15L, 15L, 35L, 15L, 25L, 25L, 15L, 15L, 15L, 15L, 5L, 5L, 
5L, 5L, 5L, 5L, 15L, 15L), Phenological_Index = c(4L, 4L, 3L, 
4L, 4L, 3L, 2L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 3L, 2L, 3L, 
3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 
3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 3L, 4L, 3L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L)), class = "data.frame", row.names = c(NA, -215L
))
r datatable discrete-mathematics arules frequency-distribution
1个回答
1
投票

这看起来像你的目标吗?

library("arules")
#> Loading required package: Matrix
#> 
#> Attaching package: 'arules'
#> The following objects are masked from 'package:base':
#> 
#>     abbreviate, write
library("tidyverse")

# Smaller tibble with only the two necessary columns
data <- tibble(
  Canopy_Index = c(15, 95, 45, 5),
  Species = c("A", "A", "B", "B"))

breaks <- seq(from = 0, to = 100, by = 10)
labels <- map_chr(breaks[1:10], ~ paste(.x, .x + 10, sep = "-"))

indices <- data$Canopy_Index
table(indices)
#> indices
#>  5 15 45 95 
#>  1  1  1  1
percents <- discretize(indices,
                       method = "fixed",
                       breaks = breaks,
                       labels = labels)
table(percents)
#> percents
#>   0-10  10-20  20-30  30-40  40-50  50-60  60-70  70-80  80-90 90-100 
#>      1      1      0      0      1      0      0      0      0      1

data %>%
  mutate(Canopy_Index_Binned = percents) %>%
  count(Canopy_Index_Binned, Species) %>%
  spread(Species, n, fill = 0)
#> # A tibble: 4 x 3
#>   Canopy_Index_Binned     A     B
#>   <fct>               <dbl> <dbl>
#> 1 0-10                    0     1
#> 2 10-20                   1     0
#> 3 40-50                   0     1
#> 4 90-100                  1     0

# There is also a base function that can convert a numeric vector to a factor in the same way:

percents <- cut(indices,
                breaks = breaks,
                labels = labels)

reprex package创建于2019-03-14(v0.2.1)

© www.soinside.com 2019 - 2024. All rights reserved.