R-beta: bugs and problems

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Tue Jun 2 11:40:20 CEST 1998


Jim Lindsey <jlindsey at luc.ac.be> writes:

> If an MS-DOS file with CRs still in place is (accidently) read into
> R using scan(), an extra zero is added to each line. This can be very
> embarassing!

Are you blaming us or yourself, then? ;)

This looks fairly easy to fix.

> 
> In manipulating repeated measurements data, I generally require
> matrices. Unfortunately, matrix factors create a lot of problems.
> Consider the following simple examples:

This has been changed for 0.62, but I'm afraid not in the direction
you'd want, but in the direction of S-plus compatibility. Basically,
matrix factors don't exist anymore: 

> > x <- as.factor(c("F","N",NA))
> > y <- cbind(x,x)
> > y[,1]
> Error: length of "levels" vector and number of levels differ
> > attributes(y)$levels <- NULL
> > is.factor(y)
> TRUE
> > y[,1]
> F N NA

R> x <- as.factor(c("F","N",NA))
R> y <- cbind(x,x)
R> y[,1]
[1]  1  2 NA
R> is.factor(y)
[1] FALSE

> > rbind(NULL,gl(2,1,2))
> Error in rbind(NULL,gl(2,1,2)): incompatible factors

R> rbind(NULL,gl(2,1,2))
     [,1] [,2]
[1,]    1    2

> 
> 3. To obtain the codes of a factor variable, one uses as.numeric().
> However, this function destroys the properties of the matrix, turning
> it into a vector.

Well...now it also works the other way...:

R> y
     [,1] [,2]
[1,]    1    1
[2,]    2    2
[3,]   NA   NA
R> as.factor(y)
[1] 1  2  NA 1  2  NA
Levels:  1 2 
R> factor(y)
[1] 1  2  NA 1  2  NA
Levels:  1 2 

Actually, a factor can have the dim attribute, but then it doesn't
show up, except in matrix ops and apply where there's an implicit
conversion to integer:

R> r
     [,1] [,2]
[1,]    1    3
[2,]    2    4
R> f<-factor(r)
R> f
[1] 1 2 3 4
Levels:  1 2 3 4 
R> dim(f)<-dim(r)
R> f
[1] 1 2 3 4
Levels:  1 2 3 4 
R> f%*%r
     [,1] [,2]
[1,]    7   15
[2,]   10   22

All this is exactly the same in Splus.

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list