Description Usage Arguments Details Value Author(s) References See Also Examples
Within each level of within, find the n^th element for which
where is TRUE, and repeat it at all positions in the level.
1 2 3 4 5 6 7 8 |
x |
vector (possibly factor) |
where |
vector, coerced to logical |
within |
a vector or list of vectors serving as an index |
n |
an integer giving the instance of interest |
... |
ignored arguments. |
nth returns, for each position in x, the n^th element of x, optionally
limiting candidate elements by where, and optionally breaking the evaluation across subsets
as specified by within. n can be 0, returning all NA, or negative,
which counts instances from the end of the vector or subsets. If n is NA, all elements are
returned. NA is returned at positions for which the condition where is not met n times.
All of x, where, and within are optional. The ‘scale’ of the function
is set to the longest of the three (member-wise for within if it is a list).
Missing x is replaced with the subscripts implied by ‘scale’.
Missing where and within are replaced with TRUE, repeated to length 'scale'.
No warnings are given if within has NA, or list members with different lengths.
first and last are convenience wrappers for nth that set n to 1
and -1, respectively. only is like first, but returns NA if a second instance exists.
distance returns subscripts less the nth subscripts, optionally considering
where and within. before, at, and after test
whether distance is less than, equal to, or greater than zero.
vector of same class as x, if supplied, else of class integer. Logical for before,
at, and after.
Tim Bergsma
http://metrumrg.googlecode.com
match
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 | first(1:10)
first(1:10,within=rep(1:2,each=5))
last( 1:10,within=rep(1:2,each=5))
first(1:10,where=0:1,within=rep(1:2,each=5))
test <- data.frame(
id=c(1,1,1,1,1,2,2,2,2,2),
time=c(1,2,3,4,5,1,2,3,4,5),
evid=c(0,1,0,1,0,1,0,1,0,1)
)
test$secondDose <- with(test,nth(where=evid==1, n=2,within=id))
test$lastDose <- with(test,nth(where=evid==1,n=-1,within=id))
test$beforeDose2 <- with(test,before(where=evid==1, n=2,within=id))
test$tafd <- with(test,time - first(time,where=evid==1,within=id))#time after first dose
test
# id time evid secondDose lastDose beforeDose2 tafd
# 1 1 1 0 4 4 TRUE -1
# 2 1 2 1 4 4 TRUE 0
# 3 1 3 0 4 4 TRUE 1
# 4 1 4 1 4 4 FALSE 2
# 5 1 5 0 4 4 FALSE 3
# 6 2 1 1 8 10 TRUE 0
# 7 2 2 0 8 10 TRUE 1
# 8 2 3 1 8 10 FALSE 2
# 9 2 4 0 8 10 FALSE 3
# 10 2 5 1 8 10 FALSE 4
only(
1:9,
where= c(0,1,0, 1,0,1, 0,0,0),
within=c(1,1,1, 2,2,2, 3,3,3)
)
#[1] 2 2 2 NA NA NA NA NA NA
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.