【问题标题】:seasonplot function in R is not a function, character or symbolR中的seasonplot函数不是函数、字符或符号
【发布时间】:2019-01-14 10:28:20
【问题描述】:
transport<- structure(list(date = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 
8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 
11L, 12L), .Label = c("01.01.2001", "01.02.2001", "01.03.2001", 
"01.04.2001", "01.05.2001", "01.06.2001", "01.07.2001", "01.08.2001", 
"01.09.2001", "01.10.2001", "01.11.2001", "01.12.2001"), class = "factor"), 
    Market_82 = c(7000L, 7272L, 7668L, 7869L, 8057L, 8428L, 8587L, 
    8823L, 8922L, 9178L, 9306L, 9439L, 3725L, 4883L, 8186L, 7525L, 
    6335L, 4252L, 5642L, 1326L, 8605L, 3501L, 1944L, 7332L), 
    transport = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
    1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L
    ), .Label = c("plane", "train"), class = "factor")), .Names = c("date", 
"Market_82", "transport"), class = "data.frame", row.names = c(NA, 
-24L))

让我们分别为每个组(飞机和火车)创建季节性图

library(forecast)
par(mfrow = c(2, 1))
lapply(split(transport['Market_82'], transport$transport), seasonplot(ts(transport,frequency=12)))

然后我得到错误

Error in match.fun(FUN) : 
  'seasonplot(ts(transport, frequency = 12))' is not a function, character or symbol

如何获得两组的季节变化图?

【问题讨论】:

    标签: r ggplot2 time-series


    【解决方案1】:

    lapply 想要一个函数,括号中没有参数。如果您想将其他参数传递给您的函数,请将它们列在函数之后,例如lapply(func, arg1, arg2).

    另外,seasonplot(ts(transport,frequency=12)) 会将 planetrain 数据绘制到一个图中。

    由于在您的示例中您还想使用ts 构建时间序列对象,因此最好将其编码在您在lapply 中定义的函数中:

    试试:

    lapply(split(transport['Market_82'], transport$transport), function(x)seasonplot(ts(x, frequency=12)))
    

    编辑

    要区分哪个组是哪个情节,您可以遍历名称:

    data = split(transport['Market_82'], transport$transport)
    par(mfrow = c(2, 1))
    lapply(names(data), function(x)seasonplot(ts(data[[x]], frequency=12), main=x))
    

    【讨论】:

    • 如何做到每张图都有一个属于哪个组的签名?
    • 现在我无法理解地块属于哪个组
    • 组在您的列表名称中给出,您无法在lapply中访问它们
    猜你喜欢
    • 2021-02-27
    • 1970-01-01
    • 2011-03-17
    • 1970-01-01
    • 1970-01-01
    • 2019-04-29
    • 2023-02-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多