-
Notifications
You must be signed in to change notification settings - Fork 114
/
arr.r
36 lines (32 loc) · 2.06 KB
/
arr.r
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
#==========================================================================================#
#==========================================================================================#
# This function is equivalent to "row" and "col", but works for arrays. Two arguments #
# are required, the array, and the dimension to extract. #
#------------------------------------------------------------------------------------------#
arr <<- function(A,d){
#----- Sanity check. -------------------------------------------------------------------#
if (! (is.array(A) || is.data.frame(A) || is.matrix(A))){
stop(" A must be an array, or at least a data frame or matrix!!!")
}else{
#----- Get dimensions and number of dimensions of A. --------------------------------#
Adim = dim(A)
n.Adim = length(Adim)
n.A = length(A)
#------------------------------------------------------------------------------------#
#---- Make sure d is a valid dimension. ---------------------------------------------#
if (! (d %wr% c(1,n.Adim))){
cat(" d must be between 1 and ",n.Adim," (# of dimensions of A","\n",sep="")
stop(" Invalid dimension request")
}#end if
#------------------------------------------------------------------------------------#
}#end if
#---------------------------------------------------------------------------------------#
#---------------------------------------------------------------------------------------#
# Get the indices of A. #
#---------------------------------------------------------------------------------------#
A.idx = arrayInd(ind=sequence(n.A),.dim=Adim)[,d]
return(A.idx)
#---------------------------------------------------------------------------------------#
}#end function arr
#==========================================================================================#
#==========================================================================================#