I am just wondering why what I am showing below might occur.

First I have an x data.frame:

> str(x)
'data.frame':	281 obs. of  2 variables:
 $ x1   : Factor w/ 5 levels "A (50-67%)","B (10-20%)",..: 1 2 5 1 2 5 1 2 5 1 ...
 $ x2   : num  33.8 60.2 6 76.8 13.8 9.4 76.9 8 15.1 78.1 ...

I need to check that for each level of factor x1 the values of x2 are (approximately) contained within a given range. In such a case I will print "ok" a third variable, otherwise I will write "err"

ifelse (x$x1 == "A (50-67%)", 
    x$check<-ifelse(x$x2<68&x$x2>49,"ok","xxxx"),
    x$check<-x$check
)
ifelse (x$x1 == "B (10-20%)",
    x$check<-ifelse(x$x2<21&x$x2>9,"ok","xxxx"),
    x$check<-x$check                        
)
ifelse (x$x1 == "C (5-15%)",
    x$check<-ifelse(x$x2<16&x$x2>4,"ok","xxxx"),
    x$check<-x$check                        
)
ifelse (x$x1 == "D (1-5%)",
    x$check<-ifelse(x$x2<6&x$x2>0,"ok","xxxx"),
    x$check<-x$check                        
)                        
ifelse (x$x1 == "E (10-20%)",
    x$check<-ifelse(x$x2<21&x$x2>9,"ok","xxxx"),
    x$check<-x$check                  
)

Now, what I obtain is not always correct - see for instance lines 111, 125, 257, 264, 272, etc in the following output:

> x
            x1   x2 check
1   A (50-67%) 33.8  xxxx
2   B (10-20%) 60.2  xxxx
3   E (10-20%)  6.0  xxxx
4   A (50-67%) 76.8  xxxx
5   B (10-20%) 13.8    ok
6   E (10-20%)  9.4    ok
7   A (50-67%) 76.9  xxxx
8   B (10-20%)  8.0  xxxx
9   E (10-20%) 15.1    ok
10  A (50-67%) 78.1  xxxx
11  E (10-20%) 21.9  xxxx
12  A (50-67%) 96.1  xxxx
13  B (10-20%)  0.6  xxxx
14  E (10-20%)  3.4  xxxx
15  A (50-67%) 88.4  xxxx
16  B (10-20%)  8.8  xxxx
17  E (10-20%)  2.8  xxxx
18  A (50-67%) 93.8  xxxx
19  B (10-20%)  0.0  xxxx
20  E (10-20%)  6.1  xxxx
21  A (50-67%) 81.3  xxxx
22  B (10-20%)  5.1  xxxx
23  E (10-20%) 13.6    ok
24  A (50-67%) 65.9  xxxx
25  B (10-20%) 14.9    ok
26  E (10-20%) 19.1    ok
27  A (50-67%) 81.2  xxxx
28  B (10-20%) 10.2    ok
29  E (10-20%)  8.6  xxxx
30  A (50-67%) 70.2  xxxx
31  B (10-20%) 20.7    ok
32  E (10-20%)  9.2    ok
33  A (50-67%) 61.4  xxxx
34  B (10-20%)  7.3  xxxx
35  E (10-20%) 31.4  xxxx
36  A (50-67%) 77.7  xxxx
37  B (10-20%)  7.9  xxxx
38  E (10-20%) 14.4    ok
39  A (50-67%) 95.0  xxxx
40  B (10-20%)  0.2  xxxx
41  E (10-20%)  4.8  xxxx
42  A (50-67%) 83.3  xxxx
43  B (10-20%)  3.8  xxxx
44  E (10-20%) 12.9    ok
45  A (50-67%) 82.2  xxxx
46  B (10-20%)  6.1  xxxx
47  E (10-20%) 11.7    ok
48  A (50-67%) 91.8  xxxx
49  B (10-20%)  0.6  xxxx
50  E (10-20%)  7.5  xxxx
51  A (50-67%) 83.8  xxxx
52  B (10-20%)  4.0  xxxx
53  E (10-20%) 12.2    ok
54  A (50-67%) 94.6  xxxx
55  B (10-20%)  0.4  xxxx
56  E (10-20%)  5.1  xxxx
57  A (50-67%) 81.4  xxxx
58  B (10-20%)  6.1  xxxx
59  E (10-20%) 12.5    ok
60  A (50-67%) 96.4  xxxx
61  E (10-20%)  3.6  xxxx
62  A (50-67%) 92.0  xxxx
63  B (10-20%)  0.5  xxxx
64  E (10-20%)  7.5  xxxx
65  A (50-67%) 81.8  xxxx
66  B (10-20%)  2.5  xxxx
67  E (10-20%) 15.7    ok
68  A (50-67%) 38.3  xxxx
69  B (10-20%)  5.9  xxxx
70   C (5-15%) 41.5  xxxx
71    D (1-5%)  1.9  xxxx
72  E (10-20%) 12.4    ok
73  A (50-67%) 96.0  xxxx
74  E (10-20%)  4.0  xxxx
75  A (50-67%) 75.9  xxxx
76  B (10-20%)  2.3  xxxx
77  E (10-20%) 21.7  xxxx
78  A (50-67%) 94.9  xxxx
79  E (10-20%)  5.1  xxxx
80  A (50-67%) 96.1  xxxx
81  E (10-20%)  3.9  xxxx
82  A (50-67%) 72.6  xxxx
83  B (10-20%)  9.4    ok
84   C (5-15%)  8.0  xxxx
85    D (1-5%)  2.4  xxxx
86  E (10-20%)  7.5  xxxx
87  A (50-67%) 48.9  xxxx
88  B (10-20%)  2.4  xxxx
89   C (5-15%) 25.0  xxxx
90    D (1-5%) 10.3    ok
91  E (10-20%) 13.3    ok
92  A (50-67%) 87.8  xxxx
93  B (10-20%)  4.1  xxxx
94   C (5-15%)  1.7  xxxx
95    D (1-5%)  0.9  xxxx
96  E (10-20%)  5.6  xxxx
97  A (50-67%) 96.9  xxxx
98  E (10-20%)  3.1  xxxx
99  A (50-67%) 72.2  xxxx
100 B (10-20%) 13.3    ok
101  C (5-15%)  5.7  xxxx
102   D (1-5%)  0.3  xxxx
103 E (10-20%)  8.5  xxxx
104 A (50-67%) 64.1  xxxx
105 B (10-20%)  9.0  xxxx
106  C (5-15%)  9.5    ok
107   D (1-5%)  1.9  xxxx
108 E (10-20%) 15.4    ok
109 A (50-67%) 45.2  xxxx
110 B (10-20%) 15.3    ok
111  C (5-15%) 18.3    ok
112   D (1-5%)  0.3  xxxx
113 E (10-20%) 20.9    ok
114 A (50-67%) 61.5  xxxx
115  C (5-15%) 22.9  xxxx
116   D (1-5%) 10.2    ok
117 E (10-20%)  5.4  xxxx
118 A (50-67%) 69.0  xxxx
119 B (10-20%)  6.1  xxxx
120  C (5-15%) 16.9    ok
121   D (1-5%)  1.0  xxxx
122 E (10-20%)  6.9  xxxx
123 A (50-67%) 61.2  xxxx
124 B (10-20%) 10.3    ok
125  C (5-15%)  5.5  xxxx
126   D (1-5%)  6.9  xxxx
127 E (10-20%) 16.2    ok
128 A (50-67%) 61.1  xxxx
129 B (10-20%) 11.5    ok
130  C (5-15%)  7.0  xxxx
131   D (1-5%)  3.9  xxxx
132 E (10-20%) 16.5    ok
133 A (50-67%) 45.9  xxxx
134 B (10-20%) 24.9  xxxx
135  C (5-15%)  0.2  xxxx
136   D (1-5%)  1.6  xxxx
137 E (10-20%) 27.4  xxxx
138 A (50-67%) 61.5  xxxx
139 B (10-20%)  8.7  xxxx
140  C (5-15%) 22.5  xxxx
141   D (1-5%)  0.1  xxxx
142 E (10-20%)  7.2  xxxx
143 A (50-67%) 64.1  xxxx
144 B (10-20%)  0.9  xxxx
145  C (5-15%) 14.4    ok
146   D (1-5%) 11.2    ok
147 E (10-20%)  9.4    ok
148 A (50-67%) 67.8  xxxx
149 B (10-20%) 11.7    ok
150  C (5-15%) 10.6    ok
151   D (1-5%)  1.3  xxxx
152 E (10-20%)  8.6  xxxx
153 A (50-67%) 65.9  xxxx
154 B (10-20%)  9.9    ok
155  C (5-15%) 11.3    ok
156   D (1-5%)  1.6  xxxx
157 E (10-20%) 11.3    ok
158 A (50-67%) 77.0  xxxx
159 B (10-20%)  5.3  xxxx
160  C (5-15%)  8.6  xxxx
161   D (1-5%)  2.6  xxxx
162 E (10-20%)  6.5  xxxx
163 A (50-67%) 77.5  xxxx
164 B (10-20%)  5.7  xxxx
165  C (5-15%)  8.1  xxxx
166   D (1-5%)  4.6  xxxx
167 E (10-20%)  4.2  xxxx
168 A (50-67%) 40.1  xxxx
169 B (10-20%) 12.9    ok
170  C (5-15%) 33.2  xxxx
171   D (1-5%)  0.3  xxxx
172 E (10-20%) 13.6    ok
173 A (50-67%) 53.9  xxxx
174 B (10-20%) 10.1    ok
175  C (5-15%)  8.4  xxxx
176   D (1-5%)  4.2  xxxx
177 E (10-20%) 23.4  xxxx
178 A (50-67%) 94.3  xxxx
179  C (5-15%)  1.7  xxxx
180 E (10-20%)  4.0  xxxx
181 A (50-67%) 62.1  xxxx
182 B (10-20%) 12.3    ok
183  C (5-15%)  5.3  xxxx
184   D (1-5%)  7.3  xxxx
185 E (10-20%) 13.0    ok
186 A (50-67%) 49.2  xxxx
187 B (10-20%) 14.1    ok
188  C (5-15%)  7.9  xxxx
189   D (1-5%)  8.9  xxxx
190 E (10-20%) 20.0    ok
191 A (50-67%) 63.6  xxxx
192 B (10-20%) 10.4    ok
193  C (5-15%) 11.9    ok
194   D (1-5%)  2.4  xxxx
195 E (10-20%) 11.7    ok
196 A (50-67%) 55.1  xxxx
197 B (10-20%) 13.5    ok
198  C (5-15%) 11.2    ok
199   D (1-5%)  4.8  xxxx
200 E (10-20%) 15.5    ok
201 A (50-67%) 68.6  xxxx
202 B (10-20%)  3.1  xxxx
203  C (5-15%)  8.2  xxxx
204   D (1-5%)  9.2    ok
205 E (10-20%) 10.8    ok
206 A (50-67%) 45.0  xxxx
207 B (10-20%)  4.8  xxxx
208  C (5-15%)  7.1  xxxx
209   D (1-5%)  4.9  xxxx
210 E (10-20%) 38.2  xxxx
211 A (50-67%) 85.2  xxxx
212 B (10-20%)  3.1  xxxx
213  C (5-15%)  4.4  xxxx
214   D (1-5%)  0.4  xxxx
215 E (10-20%)  6.9  xxxx
216 A (50-67%) 60.5  xxxx
217 B (10-20%) 10.1    ok
218  C (5-15%) 11.1    ok
219   D (1-5%)  1.8  xxxx
220 E (10-20%) 16.5    ok
221 A (50-67%) 58.7  xxxx
222 B (10-20%)  7.0  xxxx
223  C (5-15%) 10.5    ok
224   D (1-5%)  5.2  xxxx
225 E (10-20%) 18.7    ok
226 A (50-67%) 90.0  xxxx
227  C (5-15%)  5.6  xxxx
228   D (1-5%)  0.7  xxxx
229 E (10-20%)  3.8  xxxx
230 A (50-67%) 62.5  xxxx
231 B (10-20%) 13.7    ok
232  C (5-15%)  9.7    ok
233   D (1-5%)  2.6  xxxx
234 E (10-20%) 11.6    ok
235 A (50-67%) 55.6  xxxx
236 B (10-20%) 17.6    ok
237  C (5-15%) 11.8    ok
238   D (1-5%)  2.6  xxxx
239 E (10-20%) 12.4    ok
240 A (50-67%) 85.2  xxxx
241 B (10-20%)  0.6  xxxx
242  C (5-15%)  2.1  xxxx
243   D (1-5%)  2.3  xxxx
244 E (10-20%)  9.8    ok
245 A (50-67%) 87.4  xxxx
246 B (10-20%)  0.4  xxxx
247  C (5-15%)  2.9  xxxx
248   D (1-5%)  2.8  xxxx
249 E (10-20%)  6.4  xxxx
250 A (50-67%) 73.0  xxxx
251 B (10-20%)  4.0  xxxx
252  C (5-15%) 15.6    ok
253   D (1-5%)  0.7  xxxx
254 E (10-20%)  6.7  xxxx
255 A (50-67%) 90.4  xxxx
256  C (5-15%)  2.4  xxxx
257   D (1-5%)  2.5  xxxx
258 E (10-20%)  4.7  xxxx
259 A (50-67%) 64.3  xxxx
260 B (10-20%)  6.6  xxxx
261  C (5-15%) 13.3    ok
262   D (1-5%)  3.5  xxxx
263 E (10-20%) 12.3    ok
264 A (50-67%) 65.5  xxxx
265 B (10-20%) 13.5    ok
266  C (5-15%)  4.6  xxxx
267   D (1-5%)  0.9  xxxx
268 E (10-20%) 15.4    ok
269 A (50-67%) 72.1  xxxx
270 B (10-20%)  6.4  xxxx
271  C (5-15%) 12.7    ok
272   D (1-5%)  1.1  xxxx
273 E (10-20%)  7.7  xxxx
274 A (50-67%) 71.4  xxxx
275 B (10-20%)  0.9  xxxx
276  C (5-15%) 21.9  xxxx
277 E (10-20%)  5.7  xxxx
278 A (50-67%) 53.0  xxxx
279 B (10-20%)  3.6  xxxx
280  C (5-15%) 36.4  xxxx
281 E (10-20%)  7.0  xxxx

Can anyone explain why this might occur?

Thanks,
Luca


Luca Meyer
www.lucameyer.com
IBM SPSS Statistics release 19.0.0
R version 2.12.1 (2010-12-16)
Mac OS X 10.6.5 (10H574) - kernel Darwin 10.5.0







	[[alternative HTML version deleted]]

