| 1 |
\name{xapply} |
| 2 |
\alias{xapply} |
| 3 |
\title{Apply a function to ranges of variables} |
| 4 |
\description{ |
| 5 |
\code{xapply} evaluates an expression given as second argument by substituting |
| 6 |
in variables. The results are collected in a list or array in a |
| 7 |
similar way as done by \code{Sapply} or \code{lapply}. |
| 8 |
} |
| 9 |
\usage{ |
| 10 |
xapply(\dots,.sorted,simplify=TRUE,USE.NAMES=TRUE,.outer=FALSE) |
| 11 |
} |
| 12 |
\arguments{ |
| 13 |
\item{\dots}{tagged and untagged arguments. |
| 14 |
The tagged arguments define the 'variables' that are looped over, |
| 15 |
the first untagged argument defines the expression wich is |
| 16 |
evaluated. |
| 17 |
} |
| 18 |
\item{.sorted}{an optional logical value; relevant only |
| 19 |
when a range of variable is specified using the column operator |
| 20 |
"\code{:}". Decises whether variable names should be sorted |
| 21 |
alphabetically before the range of variables are created. |
| 22 |
|
| 23 |
If this argument missing, its default value is TRUE, if \code{xapply()} is called |
| 24 |
in the global environment, otherwise it is FALSE. |
| 25 |
} |
| 26 |
\item{simplify}{a logical value; should the result be simplifies in |
| 27 |
\code{Sapply}?} |
| 28 |
\item{USE.NAMES}{a logical value or a positive integer. If |
| 29 |
an integer, determines which variable is used to name |
| 30 |
the highest dimension of the result (its columns, in case is it a matrix). |
| 31 |
If TRUE, the first variable is used. |
| 32 |
} |
| 33 |
\item{.outer}{an optional logical value; if TRUE, each combination of |
| 34 |
the variables is used to evaluate the expression, |
| 35 |
if FALSE (the default) then the variables all need to have |
| 36 |
the same length and the corresponding values of the |
| 37 |
variables are used in the evaluation of the expression. |
| 38 |
} |
| 39 |
} |
| 40 |
\examples{ |
| 41 |
x <- 1:3 |
| 42 |
y <- -(1:3) |
| 43 |
z <- c("Uri","Schwyz","Unterwalden") |
| 44 |
print(x) |
| 45 |
print(y) |
| 46 |
print(z) |
| 47 |
foreach(var=c(x,y,z), # assigns names |
| 48 |
names(var) <- letters[1:3] # to the elements of x, y, and z |
| 49 |
) |
| 50 |
print(x) |
| 51 |
print(y) |
| 52 |
print(z) |
| 53 |
|
| 54 |
ds <- data.set( |
| 55 |
a = c(1,2,3,2,3,8,9), |
| 56 |
b = c(2,8,3,2,1,8,9), |
| 57 |
c = c(1,3,2,1,2,8,8) |
| 58 |
) |
| 59 |
print(ds) |
| 60 |
ds <- within(ds,{ |
| 61 |
description(a) <- "First item in questionnaire" |
| 62 |
description(b) <- "Second item in questionnaire" |
| 63 |
description(c) <- "Third item in questionnaire" |
| 64 |
|
| 65 |
wording(a) <- "What number do you like first?" |
| 66 |
wording(b) <- "What number do you like second?" |
| 67 |
wording(c) <- "What number do you like third?" |
| 68 |
|
| 69 |
foreach(x=a:c,{ # Lazy data documentation: |
| 70 |
labels(x) <- c( # a,b,c get value labels in one statement |
| 71 |
one = 1, |
| 72 |
two = 2, |
| 73 |
three = 3, |
| 74 |
"don't know" = 8, |
| 75 |
"refused to answer" = 9) |
| 76 |
missing.values(x) <- c(8,9) |
| 77 |
}) |
| 78 |
}) |
| 79 |
|
| 80 |
codebook(ds) |
| 81 |
|
| 82 |
# The colon-operator respects the order of the variables |
| 83 |
# in the data set, if .sorted=FALSE |
| 84 |
with(ds[c(3,1,2)], |
| 85 |
xapply(x=a:c, |
| 86 |
description(x) |
| 87 |
)) |
| 88 |
|
| 89 |
# Since .sorted=TRUE, the colon operator creates a range |
| 90 |
# of alphabetically sorted variables. |
| 91 |
with(ds[c(3,1,2)], |
| 92 |
xapply(x=a:c, |
| 93 |
description(x), |
| 94 |
.sorted=TRUE |
| 95 |
)) |
| 96 |
|
| 97 |
# The variables in reverse order |
| 98 |
with(ds, |
| 99 |
xapply(x=c:a, |
| 100 |
description(x) |
| 101 |
)) |
| 102 |
|
| 103 |
# The colon operator can be combined with the |
| 104 |
# concatenation function |
| 105 |
with(ds, |
| 106 |
xapply(x=c(a:b,c,c,b:a), |
| 107 |
description(x) |
| 108 |
)) |
| 109 |
|
| 110 |
# Variables can also be selected by regular expressions. |
| 111 |
with(ds, |
| 112 |
xapply(x=rx("[a-b]"), |
| 113 |
description(x) |
| 114 |
)) |
| 115 |
|
| 116 |
# Demonstrating the effects of the 'USE.NAMES' argument. |
| 117 |
with(ds, |
| 118 |
xapply(x=a:c,mean(x))) |
| 119 |
|
| 120 |
with(ds, |
| 121 |
xapply(x=a:c,mean(x), |
| 122 |
USE.NAMES=FALSE)) |
| 123 |
|
| 124 |
t(with(ds, |
| 125 |
xapply(i=1:3, |
| 126 |
x=a:c, |
| 127 |
c(Index=i, |
| 128 |
Mean=mean(x)), |
| 129 |
USE.NAMES=2))) |
| 130 |
|
| 131 |
# Result with 'simplify=FALSE' |
| 132 |
with(ds, |
| 133 |
xapply(x=a:c,mean(x), |
| 134 |
simplify=FALSE)) |
| 135 |
|
| 136 |
# It is also possible to loop over functions: |
| 137 |
xapply(fun=c(exp,log), |
| 138 |
fun(1)) |
| 139 |
|
| 140 |
# Two demonstrations for '.outer=TRUE' |
| 141 |
with(ds, |
| 142 |
xapply(x=a:c, |
| 143 |
y=a:c, |
| 144 |
cov(x,y), |
| 145 |
.outer=TRUE)) |
| 146 |
|
| 147 |
with(ds, |
| 148 |
xapply(x=a:c, |
| 149 |
y=a:c, |
| 150 |
fun=c(cov,cor), |
| 151 |
fun(x,y), |
| 152 |
.outer=TRUE)) |
| 153 |
|
| 154 |
} |
| 155 |
\keyword{programming} |