seasonplot function in R is not a function, character or symbol

Viewed 4215
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))

let's create seasonalplot for each group(plane and train) separately

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

then i get error

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

How to get seasonlap plot for two groups?

1 Answers

lapply wants a function, without the arguments in brackets. If you want to pass additional arguments to your function, list them after the function, e.g. lapply(func, arg1, arg2).

Also, seasonplot(ts(transport,frequency=12)) would plot both, plane and train data into one plot.

Since in your example you also want to build a time series object using ts, it is best to code it in a function you define within lapply:

Try:

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

Edit

To distinguish which group is for which plot, you could iterate over the names:

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))

enter image description here

Related