Debian Codegraph @

man/Foreach.Rd

raw
1 \name{foreach}
2 \alias{foreach}
3 \title{Loop over Variables in a Data Frame or Environment}
4 \description{
5 \code{foreach} evaluates an expression given as untagged argument by substituting
6 in variables. The expression may also contain assignments, which take effect in
7 the caller's environment.
8 }
9 \usage{
10 foreach(\dots,.sorted,.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{foreach()} is called
24 in the global environment, otherwise it is FALSE.
25 }
26 \item{.outer}{an optional logical value; if TRUE, each combination of
27 the variables is used to evaluate the expression,
28 if FALSE (the default) then the variables all need to have
29 the same length and the corresponding values of the
30 variables are used in the evaluation of the expression.
31 }
32 }
33 \examples{
34 x <- 1:3
35 y <- -(1:3)
36 z <- c("Uri","Schwyz","Unterwalden")
37 print(x)
38 print(y)
39 print(z)
40 foreach(var=c(x,y,z), # assigns names
41 names(var) <- letters[1:3] # to the elements of x, y, and z
42 )
43 print(x)
44 print(y)
45 print(z)
46
47 ds <- data.set(
48 a = c(1,2,3,2,3,8,9),
49 b = c(2,8,3,2,1,8,9),
50 c = c(1,3,2,1,2,8,8)
51 )
52 print(ds)
53 ds <- within(ds,{
54 description(a) <- "First item in questionnaire"
55 description(b) <- "Second item in questionnaire"
56 description(c) <- "Third item in questionnaire"
57
58 wording(a) <- "What number do you like first?"
59 wording(b) <- "What number do you like second?"
60 wording(c) <- "What number do you like third?"
61
62 foreach(x=a:c,{ # Lazy data documentation:
63 labels(x) <- c( # a,b,c get value labels in one statement
64 one = 1,
65 two = 2,
66 three = 3,
67 "don't know" = 8,
68 "refused to answer" = 9)
69 missing.values(x) <- c(8,9)
70 })
71 })
72
73 codebook(ds)
74
75 # The colon-operator respects the order of the variables
76 # in the data set, if .sorted=FALSE
77 with(ds[c(3,1,2)],
78 foreach(x=a:c,
79 print(description(x))
80 ))
81
82 # Since .sorted=TRUE, the colon operator creates a range
83 # of alphabetically sorted variables.
84 with(ds[c(3,1,2)],
85 foreach(x=a:c,
86 print(description(x)),
87 .sorted=TRUE
88 ))
89
90 # The variables in reverse order
91 with(ds,
92 foreach(x=c:a,
93 print(description(x))
94 ))
95
96 # The colon operator can be combined with the
97 # concatenation function
98 with(ds,
99 foreach(x=c(a:b,c,c,b:a),
100 print(description(x))
101 ))
102
103 # Variables can also be selected by regular expressions.
104 with(ds,
105 foreach(x=rx("[a-b]"),
106 print(description(x))
107 ))
108
109 # A demonstration for '.outer=TRUE'
110 foreach(l=letters[1:2],
111 i=1:3,
112 cat(paste0(l,i,"\n")),
113 .outer=TRUE)
114
115 }
116 \keyword{programming}