From ?data.frame:
Details:

     A data frame is a list of variables of the same length with unique
     row names, given class `"data.frame"'.

Your example constructs an object that does not conform to the definition of a data frame (the new column is not the same length as the old columns). Some data frame functions may work OK with such an object, but others will not. For example, the print function for data.frame silently handles such an illegal data frame (which could be described as unfortunate.) It would probably be far easier to construct a correct data frame in the first place than to try to find and fix functions that don't handle illegal data frames. For adding a new column to a data frame, the expressions "x[,new.column.name] <- value" and "x[[new.column.name]] <- value" will replicate the value so that the new column is the same length as the existing ones, while the "$" operator in an assignment will not replicate the value. (One could argue that this is a deficiency, but I think it has been that way for a long time, and the behavior is the same in the current version of S-plus.)


> x1 <- data.frame(a=1:3)
> x2 <- x1
> x3 <- x1
> x1$b <- 0
> x2[,"b"] <- 0
> x3[["b"]] <- 0
> sapply(x1, length)
a b
3 1
> sapply(x2, length)
a b
3 3
> sapply(x3, length)
a b
3 3
> as.matrix(x2)
a b
1 1 0
2 2 0
3 3 0
> as.matrix(x1)
Error in as.matrix.data.frame(x1) : dim<- length of dims do not match the length of object
>


At Thursday 04:50 PM 8/14/2003 +0000, Alberto Murta wrote:
Dear all

I recently noticed the following error when cohercing a data.frame into a
matrix:

> example <- matrix(1:12,4,3)
> example <- as.data.frame(example)
> example$V4 <- 0
> example
V1 V2 V3 V4
1 1 5 9 0
2 2 6 10 0
3 3 7 11 0
4 4 8 12 0
> example <- as.matrix(example)
Error in as.matrix.data.frame(example) : dim<- length of dims do not match the
length of object


However, if the column to be added has the right number of lines, there's no
error:

> example <- matrix(1:12,4,3)
> example <- as.data.frame(example)
> example$V4 <- rep(0,4)
> example
  V1 V2 V3 V4
1  1  5  9  0
2  2  6 10  0
3  3  7 11  0
4  4  8 12  0
> example <- as.matrix(example)
> example
  V1 V2 V3 V4
1  1  5  9  0
2  2  6 10  0
3  3  7 11  0
4  4  8 12  0

Shouldn't it work well both ways? I checked the attributes and dims of the
data frame and they are the same in both cases. Where's the difference that
originates the error message?
Thanks in advance

Alberto

platform i686-pc-linux-gnu
arch     i686
os       linux-gnu
system   i686, linux-gnu
status
major    1
minor    7.1
year     2003
month    06
day      16
language R


-- Alberto G. Murta Institute for Agriculture and Fisheries Research (INIAP-IPIMAR) Av. Brasilia, 1449-006 Lisboa, Portugal | Phone: +351 213027062 Fax:+351 213015948 | http://www.ipimar-iniap.ipimar.pt/pelagicos/

______________________________________________
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help

______________________________________________ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help

Reply via email to