[Rd] unexpected '[<-.data.frame' result

Peter Dalgaard p.dalgaard at biostat.ku.dk
Wed Oct 26 22:25:32 CEST 2005


"Charles C. Berry" <cberry at tajo.ucsd.edu> writes:

> Is this a bug?
> 
> If not, I am curious to know why '[<-.data.frame' was designed to yield 
> a.frame$y != a.frame$z rather than refusing to carry out the operation at 
> all.
> 
> > a.frame <- data.frame( x=letters[1:5] )
> > a.frame[ 2:5, "y" ] <- letters[2:5]
> > a.frame[[ "z" ]][ 2:5 ] <- letters[2:5]
> > a.frame
>    x    y    z
> 1 a    b <NA>
> 2 b    c    b
> 3 c    d    c
> 4 d    e    d
> 5 e <NA>    e

It sure looks like a bug, and we're not even prototype-compatible:

turmalin:~/>Splus
S-PLUS : Copyright (c) 1988, 2003 Insightful Corp.
S : Copyright Lucent Technologies, Inc.
Version 6.2.1  for Linux 2.4.18 : 2003
Working data will be in /home/bs/pd/MySwork
> a.frame <- data.frame( x=letters[1:5] )
>  a.frame[ 2:5, "y" ] <- letters[2:5]
> a.frame
  x y
1 a
2 b b
3 c c
4 d d
5 e e

(S-PLUS doesn't know about character NA, so it's not surprising that
you get an empty cell, but it does fill in the right rows of y).

Why would you expect the operation to be refused?

The problem is not, BTW, restricted to character column indexing:

> a.frame
  x y   V3
1 a b    b
2 b b    c
3 c c    d
4 d d    e
5 e e <NA>

The problem would seem to be the else clause in

            if (jj <= nvars) {
                if (length(dim(x[jj])) != 2)
                  x[[jj]][iseq] <- vjj
                else x[[jj]][iseq, ] <- vjj
            }
            else {
                length(vjj) <- nrows
                x[[jj]] <- vjj
            }

which I think wants to be

            else {
                x[[jj]] <- vjj[FALSE]
                length(x[[jj]]) <- nrows
                x[[jj]][iseq] <- vjj
            }



-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)                  FAX: (+45) 35327907



More information about the R-devel mailing list