Site hosted by Angelfire.com: Build your free website today!

Working With Missing Values :

> q <- matrix(c(4,3,2,3,4,2,3,4,1,2,4,1,2,4,1,2,3,4,2,4),ncol=4,byrow=T)
> q
     [,1] [,2] [,3] [,4]
[1,]    4    3    2    3
[2,]    4    2    3    4
[3,]    1    2    4    1
[4,]    2    4    1    2
[5,]    3    4    2    4
> attr(q,"dim")
[1] 5 4
> w <- rbind (q, matrix(c(1,4,6,8,3,4,5,7),2,4,byrow=T))
> w
     [,1] [,2] [,3] [,4]
[1,]    4    3    2    3
[2,]    4    2    3    4
[3,]    1    2    4    1
[4,]    2    4    1    2
[5,]    3    4    2    4
[6,]    1    4    6    8
[7,]    3    4    5    7
> u <- cbind (w, matrix(c(1,2,3,4,5,6,7,8,9,0,1,2,3,4),7,2,byrow=F))
> u
     [,1] [,2] [,3] [,4] [,5] [,6]
[1,]    4    3    2    3    1    8
[2,]    4    2    3    4    2    9
[3,]    1    2    4    1    3    0
[4,]    2    4    1    2    4    1
[5,]    3    4    2    4    5    2
[6,]    1    4    6    8    6    3
[7,]    3    4    5    7    7    4
> u[2,2]<- Inf
> u[4,2]<- Inf
> u[5,2]<- -Inf
> u[5,3]<- NA
> u[3,3]<- NA
> u
      [,1]  [,2]  [,3]  [,4]  [,5]  [,6]
[1,]     4     3     2     3     1     8
[2,]     4   Inf     3     4     2     9
[3,]     1    2    NA     1     3     0
[4,]     2   Inf     1     2     4     1
[5,]     3  -Inf    NA     4     5     2
[6,]     1     4     6     8     6     3
[7,]     3     4     5     7     7     4
> nrow(u)
[1] 7
> ncol(u)
[1] 6
> u[,2]
[1]    3  Inf    2  Inf -Inf    4    4
> v1 <- is.inf(u[,2])
> v1
[1] F T F T T F F
> max(u[,2][!v1])                     # [Excludes infinite values]
[1] 4
> u[,3]
[1]  2  3 NA  1 NA  6  5
> v2 <- is.na(u[,3])
> v2
[1] F F T F T F F
> max(u[,3][!v2])                     # [Excludes Not Available values]
[1] 6
> mean(u[,3][!v2])
[1] 3.4
> dimnames(u)<-list(c("row1","row2","row3","row4","row5","row6","row7"),c("col1","col2",
+ "col3","col4","col5","col6"))
> u
      col1  col2  col3  col4  col5  col6
row1     4     3     2     3     1 8
row2     4   Inf     3     4     2     9
row3     1     2    NA     1    3     0
row4     2   Inf     1     2     4     1
row5     3  -Inf    NA     4     5     2
row6     1     4     6     8     6     3
row7     3     4     5     7     7     4
> w1<- u[,"col2"]
> w1
 row1 row2 row3 row4 row5 row6 row7
    3  Inf    2  Inf -Inf    4    4
> is.inf(w1)
 row1  row2  row3  row4  row5  row6  row7
   F     T     F     T     T     F     F
> w2<- u[,"col3"]
> w2
 row1 row2 row3 row4 row5 row6 row7
    2    3   NA    1   NA    6    5
> is.na(w2)
 row1  row2  row3  row4  row5  row6  row7
    F     F     T     F     T     F     F

Back to Index page