tapply(x,head(rep(seq(ceiling(length(x [x)/ 5)),each = 5),length(x)),峰度)中的错误):找不到对象'峰度'

时间:2018-09-25 07:21:59

标签: r

我需要使用tapply函数为给定数据集中的每个连续独立bin /窗口计算峰度和偏度。

data <- data.frame(Time = c("09:01:01", "09:01:02", "09:01:03", "09:01:04", "09:01:05", "09:01:06", "09:01:07", "09:01:08",
                      "09:01:09", "09:01:10", "09:01:11", "09:01:12", "09:01:13", "09:01:14", "09:01:15", "09:01:16",
                      "09:01:17", "09:01:18", "09:01:19", "09:01:20"),
             variable = c(36, 1, 46, 37, 29, 38, 11, 56, 45, 28, 6, 9, 51, 27, 38, 43, 16, 19, 33, 44))

data

  Time        variable
  09:01:01     36 
  09:01:02     1
  09:01:03     46
  09:01:04     37 
  09:01:05     29
  09:01:06     38
  09:01:07     11
  09:01:08     56
  09:01:09     45
  09:01:10     28
  09:01:11     6
  09:01:12     9
  09:01:13     51
  09:01:14     27
  09:01:15     38
  09:01:16     43
  09:01:17     16
  09:01:18     19
  09:01:19     33
  09:01:20     44

所以我用下面的代码来计算偏度和峰度

x <- data$variable
a <- tapply(x, head(rep(seq(ceiling(length(x)/5)), each=5),length(x)), kurtosis)
b <- tapply(x, head(rep(seq(ceiling(length(x)/5)), each=5),length(x)), skewness)

Error in tapply(x, head(rep(seq(ceiling(length(x)/5)), each = 5), length(x)),  : object 'kurtosis' not found

预期结果应为:均值

Time        variable
  09:01:01     29.8 
  09:01:06     35.6
  09:01:11     26.2
  09:01:16     31

偏斜度的预期结果应为

Time        variable
  09:01:01     -1.55899
  09:01:06     -0.49703
  09:01:11     0.213318
  09:01:16     -0.21706

预先感谢

1 个答案:

答案 0 :(得分:0)

因此有效:

data <- data.frame(Time = c("09:01:01", "09:01:02", "09:01:03", "09:01:04", "09:01:05", "09:01:06", "09:01:07", "09:01:08",
      "09:01:09", "09:01:10", "09:01:11", "09:01:12", "09:01:13", "09:01:14", "09:01:15", "09:01:16",
      "09:01:17", "09:01:18", "09:01:19", "09:01:20"),
   variable = c(36, 1, 46, 37, 29, 38, 11, 56, 45, 28, 6, 9, 51, 27, 38, 43, 16, 19, 33, 44), 
   stringsAsFactors=FALSE
)
library("e1071")
data$group <- gl(n=nrow(data)%/%5, k=5, length=nrow(data))

data.frame(Time=tapply(data$Time, data$group, `[`, 1),
           m =tapply(data$variable, data$group, mean), 
           s1=tapply(data$variable, data$group, FUN=skewness), 
           s2=tapply(data$variable, data$group, FUN=skewness, type=2),
           k1=tapply(data$variable, data$group, FUN=kurtosis),
           k2=tapply(data$variable, data$group, FUN=kurtosis, type=2)
)
#       Time    m         s1         s2        k1           k2
# 1 09:01:01 29.8 -0.7483143 -1.5589882 -1.259323  2.879232018
# 2 09:01:06 35.6 -0.2385734 -0.4970280 -1.719941  0.000368498
# 3 09:01:11 26.2  0.1023926  0.2133180 -2.020074 -1.875465251
# 4 09:01:16 31.0 -0.1041885 -0.2170593 -2.179430 -2.871439621

with(data, data.frame(Time=tapply(Time, group, `[`, 1),
           m =tapply(variable, group, mean), 
           s1=tapply(variable, group, FUN=skewness), 
           s2=tapply(variable, group, FUN=skewness, type=2),
           k1=tapply(variable, group, FUN=kurtosis),
           k2=tapply(variable, group, FUN=kurtosis, type=2)
))