Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#' Split an array into list by a given array dimension
#'
#'@description This function splits an array into a list as required by PlotLayout function from s2dv when 'special_args' parameter is used. The function ArrayToList allows to add names to the elements of the list in two different levels the 'list' or the 'sublist'.
#'
#'@param data A multidimensional array.
#'@param dim A character string indicating the name of the dimension to split or an integer indicating the position of the dimension.
#'@param level A string character 'list' or 'sublist' indicating if it should be a list or a sublist. By default it creates a list.
#'@param names A vector of character strings to name the list (if it is a single string, it would be reused) or a single character string to name the elements in the sublist.
#'
#'@return A list of arrays of the length of the dimension set in parameter 'dim'.
#'
#'@examples
#'data <- array(1:240, c(month = 12, member = 5, time = 4))
#'# Create a list:
#'datalist <- ArrayToList(data, dim = 'month', level = 'list', names = month.name)
#'class(datalist)
#'class(datalist[[1]])
#'str(datalist)
#'# Create a sublist:
#'datalist <- ArrayToList(data, dim = 'month', level = 'sublist', names = 'dots')
#'class(datalist)
#'class(datalist[[1]])
#'class(datalist[[1]][[1]])
#'str(datalist)
#'@seealso \link[s2dv]{PlotLayout}
#'@export
ArrayToList <- function(data, dim, level = 'list', names = NULL) {
if (is.null(dim(data))) {
stop("Parameter 'data' must be an array or matrix.")
}
if (length(dim) > 1) {
warning("The lenght of parameter 'dim' is greater than 1 and only the",
" first element is used.")
dim <- dim[1]
}
if (is.character(dim)) {
dim <- which(names(dim(data)) == dim)
}
if( !is.character(names)) {
stop("Parameter 'names' is not class character.")
}
datalist <- asplit(data, dim)
attributes(datalist) <- NULL
if (level == 'list') {
if (length(names) == 1) {
names <- rep(names, dim(data)[dim])
} else {
if (length(names) != dim(data)[dim]) {
stop("The length of parameter names could be 1 or equal to the dimension to split.")
}
}
names(datalist) <- names
} else if (level == 'sublist') {
if (length(names) > 1) {
warning("Parameter 'names' should be a single character string to name the sublist.",
"Only the first element is used.")
names <- names[1]
}
datalist <- lapply(datalist, function(x) {
res <- list(x)
names(res) <- names
return(res)})
} else {
stop("Parameter 'level' should be 'list' nor 'sublist'.")
}
return(datalist)
}