get_val {lazy.tools} | R Documentation |
Get Values from an Array
get_val(A, i = NULL, j = NULL, k = NULL, l = NULL, subs = NULL)
put_val(A, i = NULL, j = NULL, k = NULL, l = NULL, subs = NULL) <- value
A |
An array object |
i |
Vector consisting of the subscripts for the 1st dimension |
j |
Vector consisting of the subscripts for the 2nd dimension |
k |
Vector consisting of the subscripts for the 3rd dimension |
l |
Vector consisting of the subscripts for the 4th dimension |
subs |
string specifying the subscripts such as "1:2, , 4, 2:2" |
value |
The value to be assigned. |
It is assumed that A
is an array.
When A
is a list of data frames, use get_valL
instead.
When numeric subs
is given it must be a vector of length
length(dim(A))
specifying a scalar element of A
.
get_val(A,i,j,k,l)
returns A[i,j,k,l]
.
put_val(A,i,j,k,l) <- value
puts the value at A[i,j,k,l]
.
Therefore, when A
is an array, better use A[i,j,k,l]
.
get_val(A,i,j,k,l)
is equivalent to get_val(A,subs=subs)
where subs
contains the values of i,j,k,l
as a string.
For example,
get_val(A, 1:2, , 3)
is equivalent to
get_val(A, subs="1:2, , 3")
.
Use subs=
when the # of dimensions is more than 5.
subs=
has priority over i,j,k,l
.
The result will be simplified to a matrix or vector if possible.
get_val(A, i,j.,k,l)
returns
A[i,j,k,l]
.
put_val(A, i,j,k,l) <- value
replaces
A[i,j,k,l]
with value
.
# set up an array B
dims=c(5,4,3,2)
B <- array(0,dims)
B[,,,1] <- demomat(5,4,3)*10+1
B[,,,2] <- demomat(5,4,3)*10+2
dimnames(B) <-
mapply( function(x,y){paste(x,1:y,sep="")}, letters[1:length(dims)],dims)
# get B[5,4,3,2]
res1=get_val(B,5,4,3,2)
res2=get_val(B,subs=c(5,4,3,2))
res3=get_val(B,subs="5,4,3,2")
Print(res1,res2,res3)
# get B[2,,3,]: cannot use numeric subs here.
res1=get_val(B,2,,3)
res3=get_val(B,subs="2,,3,")
Print(res1, res3 )
# modify B[,1,2:3,1]
( temp <- B[,1,2:3,1] )
put_val(B,,1,2:3,1) <- -9999
B[,1,2:3,1]
put_val(B,subs=",1,2:3,1") <- temp
B[,1,2:3,1]
put_val(B,subs=c(1,1,1,2)) <- -9999
B[1,1,1,2]