Learn R Programming

spray (version 1.0-26)

Extract.spray: Extract or Replace Parts of a spray

Description

Extract or replace subsets of sprays.

Usage

# S3 method for spray
[(S, ..., drop=FALSE)
# S3 method for spray
[(S, index, ...) <- value

Arguments

S

A spray object

index

elements to extract or replace

value

replacement value

...

Further arguments

drop

Boolean, with default FALSE meaning to return a spray object and TRUE meaning to drop the spray structure and return a numeric vector

Details

These methods should work as expected, although the off-by-one issue might be a gotcha. disordR discipline is enforced where appropriate.

In S[index,...], argument drop is FALSE by default, in which case a spray object is returned. If drop is TRUE a numeric vector is returned, with elements corresponding to the rows of index. Compare coeffs(S), which returns a disord object; in S[index,drop=TRUE], the rows of index specify a unique order for the return value.

If a <- spray(diag(3)), for example, then idiom such as a[c(1,2,3)] cannot work, because one would like a[1,2,3] and a[1:3,2,3] to work.

If p <- 1:3, then one might expect idiom such as S[1,,p,1:3] to work but this is problematic and a discussion is given in inst/missing_accessor.txt.

Functions spray_extract_disord() and spray_replace_disord() are low-level helper functions which implement idiom such as a[coeffs(a) < 3] anda[coeffs(a) < 3] <- 99.

Examples

Run this code
(a <- spray(diag(5)))
a[rbind(rep(1,5))] <- 5
a

a[3,4,5,3,1]  # the NULL polynomial

a[0,1,0,0,0]
a[0,1,0,0,0,drop=TRUE]

a[2,3:5,4,3,3] <- 9
a

options(polyform = TRUE)   # print as a multivariate polynomial
a

options(polyform = FALSE)  # print in sparse array form
a

(S1 <- spray(diag(5),1:5))
(S2 <- spray(1-diag(5),11:15))
(S3 <- spray(rbind(c(1,0,0,0,0),c(1,2,1,1,1))))

S1[] <- 3
S1[] <- S2

S1[S3] <- 99
S1

S <- rspray()
S[coeffs(S) > 4]
S[coeffs(S) < 6] <- 99
S


Run the code above in your browser using DataLab