我在R中键入了模式函数,但结果很奇怪。发生了什么?

时间:2018-04-17 07:01:25

标签: r mode

在R中创建频率表后;然后我输入了一个模式函数,如下所示。

#Creating mode function by class(or group) in R
mode <- function(x) {
  uni <- unique(x)
  uni[which.max(tabulate(match(x,uni)))]
}

但是,模式结果显示奇怪的范围,而不是模式范围。实际上,在这种情况下,模式应该是&#34; [194,215)。&#34;

#Showing the mode result of the range in a data frame
mode(x$Range)
"[0,21.5)"

这是我的&#34; x&#34;数据

x <- data.frame(Range = c("[0,21.5)", "[21.5,43)", "[43,64.6)", "[64.6,86.1)", "[86.1,108)", "[108,129)", "[129,151)", "[151,172)", "[172,194)", "[194,215)", "[215,237)", "[237,258)", "[258,280)", "[280,301)", "[301,323)", "[323,344)", "[344,366)", "[366,387)", "[387,409)", "[409,430)", "[430,452)", "[452,473)", "[473,495)", "[495,516)", "[516,538)", "[538,560)", "[560,581)", "[581,603)", "[603,624)", "[624,646)", "[646,667)", "[667,689)", "[689,710)", "[710,732)", "[732,753)", "[753,775)", "[775,796)", "[796,818)", "[818,839)", "[839,861)", "[861,882)", "[882,904)", "[904,925)", "[925,947)", "[947,968)", "[968,990)", "[990,1.01e+03)", "[1.01e+03,1.03e+03)", "[1.03e+03,1.05e+03)", "[1.05e+03,1.08e+03)", "[1.08e+03,1.1e+03)", "[1.1e+03,1.12e+03)", "[1.12e+03,1.14e+03)", "[1.14e+03,1.16e+03)", "[1.16e+03,1.18e+03)", "[1.18e+03,1.21e+03)", "[1.21e+03,1.23e+03)", "[1.23e+03,1.25e+03)", "[1.25e+03,1.27e+03)", "[1.27e+03,1.29e+03)", "[1.29e+03,1.31e+03)", "[1.31e+03,1.33e+03)", "[1.33e+03,1.36e+03)", "[1.36e+03,1.38e+03)", "[1.38e+03,1.4e+03)", "[1.4e+03,1.42e+03)", "[1.42e+03,1.44e+03)", "[1.44e+03,1.46e+03)", "[1.46e+03,1.48e+03)", "[1.48e+03,1.51e+03)", "[1.51e+03,1.53e+03)", "[1.53e+03,1.55e+03)", "[1.55e+03,1.57e+03)", "[1.57e+03,1.59e+03)", "[1.59e+03,1.61e+03)", "[1.61e+03,1.64e+03)", "[1.64e+03,1.66e+03)", "[1.66e+03,1.68e+03)", "[1.68e+03,1.7e+03)", "[1.7e+03,1.72e+03)", "[1.72e+03,1.74e+03)", "[1.74e+03,1.76e+03)", "[1.76e+03,1.79e+03)", "[1.79e+03,1.81e+03)", "[1.81e+03,1.83e+03)", "[1.83e+03,1.85e+03)", "[1.85e+03,1.87e+03)", "[1.87e+03,1.89e+03)", "[1.89e+03,1.92e+03)", "[1.92e+03,1.94e+03)", "[1.94e+03,1.96e+03)", "[1.96e+03,1.98e+03)", "[1.98e+03,2e+03)", "[2e+03,2.02e+03)", "[2.02e+03,2.04e+03)", "[2.04e+03,2.07e+03)", "[2.07e+03,2.09e+03)", "[2.09e+03,2.11e+03)", "[2.11e+03,2.13e+03)", "[2.13e+03,2.15e+03]"), Counts = c(2815, 5906, 5291, 8814, 9687, 16871, 13291, 15030, 11929, 17136, 10343, 10240, 10027, 5436, 5177, 4758, 2785, 2576, 2280, 1880, 1128, 1347, 951, 975, 801, 731, 744, 705, 913, 291, 869, 225, 422, 304, 263, 180, 81, 363, 349, 36, 120, 226, 20, 158, 114, 205, 37, 71, 86, 419, 35, 23, 0, 61, 50, 15, 41, 62, 15, 0, 0, 15, 10, 75, 0, 1, 0, 0, 8, 0, 0, 0, 0, 0, 0, 2, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 22, 0, 10, 1, 0, 0, 0, 0, 0, 0, 15))

我需要你的帮助。没有错误消息,但输出不正确。 : - (

dput格式的数据。

x <-
structure(list(Range = structure(c(1L, 59L, 71L, 82L, 93L, 48L, 
49L, 50L, 51L, 52L, 60L, 61L, 62L, 63L, 65L, 66L, 67L, 68L, 69L, 
70L, 72L, 73L, 74L, 75L, 76L, 77L, 78L, 79L, 80L, 81L, 83L, 84L, 
85L, 86L, 87L, 88L, 89L, 90L, 91L, 92L, 94L, 95L, 96L, 97L, 98L, 
99L, 100L, 2L, 3L, 4L, 5L, 10L, 6L, 7L, 8L, 9L, 11L, 12L, 13L, 
14L, 15L, 16L, 17L, 18L, 19L, 24L, 20L, 21L, 22L, 23L, 25L, 26L, 
27L, 28L, 29L, 30L, 31L, 32L, 33L, 38L, 34L, 35L, 36L, 37L, 39L, 
40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 64L, 53L, 54L, 55L, 56L, 
57L, 58L), .Label = c("[0,21.5)", "[1.01e+03,1.03e+03)", "[1.03e+03,1.05e+03)", 
"[1.05e+03,1.08e+03)", "[1.08e+03,1.1e+03)", "[1.12e+03,1.14e+03)", 
"[1.14e+03,1.16e+03)", "[1.16e+03,1.18e+03)", "[1.18e+03,1.21e+03)", 
"[1.1e+03,1.12e+03)", "[1.21e+03,1.23e+03)", "[1.23e+03,1.25e+03)", 
"[1.25e+03,1.27e+03)", "[1.27e+03,1.29e+03)", "[1.29e+03,1.31e+03)", 
"[1.31e+03,1.33e+03)", "[1.33e+03,1.36e+03)", "[1.36e+03,1.38e+03)", 
"[1.38e+03,1.4e+03)", "[1.42e+03,1.44e+03)", "[1.44e+03,1.46e+03)", 
"[1.46e+03,1.48e+03)", "[1.48e+03,1.51e+03)", "[1.4e+03,1.42e+03)", 
"[1.51e+03,1.53e+03)", "[1.53e+03,1.55e+03)", "[1.55e+03,1.57e+03)", 
"[1.57e+03,1.59e+03)", "[1.59e+03,1.61e+03)", "[1.61e+03,1.64e+03)", 
"[1.64e+03,1.66e+03)", "[1.66e+03,1.68e+03)", "[1.68e+03,1.7e+03)", 
"[1.72e+03,1.74e+03)", "[1.74e+03,1.76e+03)", "[1.76e+03,1.79e+03)", 
"[1.79e+03,1.81e+03)", "[1.7e+03,1.72e+03)", "[1.81e+03,1.83e+03)", 
"[1.83e+03,1.85e+03)", "[1.85e+03,1.87e+03)", "[1.87e+03,1.89e+03)", 
"[1.89e+03,1.92e+03)", "[1.92e+03,1.94e+03)", "[1.94e+03,1.96e+03)", 
"[1.96e+03,1.98e+03)", "[1.98e+03,2e+03)", "[108,129)", "[129,151)", 
"[151,172)", "[172,194)", "[194,215)", "[2.02e+03,2.04e+03)", 
"[2.04e+03,2.07e+03)", "[2.07e+03,2.09e+03)", "[2.09e+03,2.11e+03)", 
"[2.11e+03,2.13e+03)", "[2.13e+03,2.15e+03]", "[21.5,43)", "[215,237)", 
"[237,258)", "[258,280)", "[280,301)", "[2e+03,2.02e+03)", "[301,323)", 
"[323,344)", "[344,366)", "[366,387)", "[387,409)", "[409,430)", 
"[43,64.6)", "[430,452)", "[452,473)", "[473,495)", "[495,516)", 
"[516,538)", "[538,560)", "[560,581)", "[581,603)", "[603,624)", 
"[624,646)", "[64.6,86.1)", "[646,667)", "[667,689)", "[689,710)", 
"[710,732)", "[732,753)", "[753,775)", "[775,796)", "[796,818)", 
"[818,839)", "[839,861)", "[86.1,108)", "[861,882)", "[882,904)", 
"[904,925)", "[925,947)", "[947,968)", "[968,990)", "[990,1.01e+03)"
), class = "factor"), Counts = c(2815, 5906, 5291, 8814, 9687, 
16871, 13291, 15030, 11929, 17136, 10343, 10240, 10027, 5436, 
5177, 4758, 2785, 2576, 2280, 1880, 1128, 1347, 951, 975, 801, 
731, 744, 705, 913, 291, 869, 225, 422, 304, 263, 180, 81, 363, 
349, 36, 120, 226, 20, 158, 114, 205, 37, 71, 86, 419, 35, 23, 
0, 61, 50, 15, 41, 62, 15, 0, 0, 15, 10, 75, 0, 1, 0, 0, 8, 0, 
0, 0, 0, 0, 0, 2, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 22, 
0, 10, 1, 0, 0, 0, 0, 0, 0, 15)), .Names = c("Range", "Counts"
), row.names = c(NA, -100L), class = "data.frame")

0 个答案:

没有答案