The R Project SVN R

Rev

Rev 88581 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 88581 Rev 88896
Line 135... Line 135...
135
\references{
135
\references{
136
  \bibshow{R:Becker+Chambers+Wilks:1988}
136
  \bibshow{R:Becker+Chambers+Wilks:1988}
137
}
137
}
138
\examples{
138
\examples{
139
require(stats) # for rpois and xtabs
139
require(stats) # for rpois and xtabs
-
 
140
 
140
## Simple frequency distribution
141
## Simple frequency distribution
141
table(rpois(100, 5))
142
table(rpois(100, 5))
-
 
143
 
142
## Check the design:
144
## Check the design:
143
with(warpbreaks, table(wool, tension))
145
with(warpbreaks, table(wool, tension))
144
table(state.division, state.region)
146
table(state.division, state.region)
145
 
147
 
146
# simple two-way contingency table
148
## Simple two-way contingency table:
147
with(airquality, table(cut(Temp, quantile(Temp)), Month))
149
with(airquality, table(cut(Temp, quantile(Temp)), Month))
148
 
150
 
-
 
151
## Dimension names:
149
a <- letters[1:3]
152
a <- letters[1:3]
150
table(a, sample(a))                    # dnn is c("a", "")
153
table(a, sample(a))                    # dnn is c("a", "")
151
table(a, sample(a), dnn = NULL)        # dimnames() have no names
154
table(a, sample(a), dnn = NULL)        # dimnames() have no names
152
table(a, sample(a), deparse.level = 0) # dnn is c("", "")
155
table(a, sample(a), deparse.level = 0) # dnn is c("", "")
153
table(a, sample(a), deparse.level = 2) # dnn is c("a", "sample(a)")
156
table(a, sample(a), deparse.level = 2) # dnn is c("a", "sample(a)")
Line 158... Line 161...
158
class(tab <- xtabs(Freq ~ ., DF)) # xtabs & table
161
class(tab <- xtabs(Freq ~ ., DF)) # xtabs & table
159
## tab *is* "the same" as the original table:
162
## tab *is* "the same" as the original table:
160
all(tab == UCBAdmissions)
163
all(tab == UCBAdmissions)
161
all.equal(dimnames(tab), dimnames(UCBAdmissions))
164
all.equal(dimnames(tab), dimnames(UCBAdmissions))
162
 
165
 
-
 
166
## Excluding levels:
163
a <- rep(c(NA, 1/0:3), 10)
167
a <- rep(c(NA, 1/0:3), 10)
164
table(a)                 # does not report NA's
168
table(a)                 # does not report NAs
165
table(a, exclude = NULL) # reports NA's
169
table(a, exclude = NULL) # reports NAs
166
b <- factor(rep(c("A","B","C"), 10))
170
b <- factor(rep(c("A","B","C"), 10))
167
table(b)
171
table(b)
168
table(b, exclude = "B")
172
table(b, exclude = "B")
169
d <- factor(rep(c("A","B","C"), 10), levels = c("A","B","C","D","E"))
173
d <- factor(rep(c("A","B","C"), 10), levels = c("A","B","C","D","E"))
170
table(d, exclude = "B")
174
table(d, exclude = "B")
Line 173... Line 177...
173
## NA counting:
177
## NA counting:
174
is.na(d) <- 3:4
178
is.na(d) <- 3:4
175
d. <- addNA(d)
179
d. <- addNA(d)
176
d.[1:7]
180
d.[1:7]
177
table(d.) # ", exclude = NULL" is not needed
181
table(d.) # ", exclude = NULL" is not needed
178
## i.e., if you want to count the NA's of 'd', use
182
## i.e., if you want to count the NAs of 'd', use
179
table(d, useNA = "ifany")
183
table(d, useNA = "ifany")
180
 
184
 
181
## "pathological" case:
185
## "pathological" case:
182
d.patho <- addNA(c(1,NA,1:2,1:3))[-7]; is.na(d.patho) <- 3:4
186
d.patho <- addNA(c(1,NA,1:2,1:3))[-7]; is.na(d.patho) <- 3:4
183
d.patho
187
d.patho
184
## just 3 consecutive NA's ? --- well, have *two* kinds of NAs here :
188
## just 3 consecutive NAs ? --- well, have *two* kinds of NAs here :
185
as.integer(d.patho) # 1 4 NA NA 1 2
189
as.integer(d.patho) # 1 4 NA NA 1 2
186
##
190
##
187
## In R >= 3.4.0, table() allows to differentiate:
191
## In R >= 3.4.0, table() allows to differentiate:
188
table(d.patho)                   # counts the "unusual" NA
192
table(d.patho)                   # counts the "unusual" NA
189
table(d.patho, useNA = "ifany")  # counts all three
193
table(d.patho, useNA = "ifany")  # counts all three