From gregor.gorjanc at bfro.uni-lj.si  Mon Feb 14 12:21:43 2005
From: gregor.gorjanc at bfro.uni-lj.si (Gregor GORJANC)
Date: Mon Feb 14 12:22:05 2005
Subject: [Rd] corrupt data frame: columns will be truncated or padded
	with NAs in: format.data.frame(x, digits = digits)
In-Reply-To: <Pine.LNX.4.61.0502140725570.22249@gannet.stats>
References: <7FFEE688B57D7346BC6241C55900E730B6FF14@pollux.bfro.uni-lj.si>
	<Pine.LNX.4.61.0502140725570.22249@gannet.stats>
Message-ID: <421089C7.8080401@bfro.uni-lj.si>

Hello!

Prof Brian Ripley wrote:
> You did create a corrupt data frame by using *replacement* on part of 
> something that did not exist.  The simple workaround is not to do that. 
> One can argue about what should happen in such a case and currently R 
> assumes that you know what you are doing and will only treat the data 
> frame as a list. We could make this an error, but that would add an 
> overhead to be paid by careful users too.
I agree to some extent, however I was very surprised of this behaviour. I 
often deal with data that have missing values and now I really do not know 
how to manage such data. How can one add a column to existing data frame
in such a way, that you don't get corrupted data frames as in my example?

(tmp <- data.frame(y1=1:4, f1=factor(c("A", "B", "C", "D"))))
   y1 f1
1  1  A
2  2  B
3  3  C
4  4  D
# Add new column, which is not full (missing some data for last
# records)
tmp[1:2, "y2"] <- 2
tmp
  y1 f1   y2
1  1  A    2
2  2  B    2
3  3  C <NA>
4  4  D <NA>
Warning message:
corrupt data frame: columns will be truncated or padded with NAs
in: format.data.frame(x, digits = digits)

I hope that this is not the best solution:
tmp <- data.frame(y1=1:4, f1=factor(c("A", "B", "C", "D")))
tmp$y2 <- NA
tmp[1:2, "y2"] <- 2
tmp

> If you really want to understand what is going on here, please read the 
> source code: R is a volunteer project and the volunteers do not have 
> time to explain each and every one of your error messages to you -- we 
> have already had several goes over including data frames in data frames.
I try to and I hope I did not take to much of your time.

[... removed the rest ...]

-- 
Lep pozdrav / With regards,
     Gregor GORJANC

---------------------------------------------------------------
University of Ljubljana
Biotechnical Faculty       URI: http://www.bfro.uni-lj.si
Zootechnical Department    mail: gregor.gorjanc <at> bfro.uni-lj.si
Groblje 3                  tel: +386 (0)1 72 17 861
SI-1230 Domzale            fax: +386 (0)1 72 17 888
Slovenia


[Rd] corrupt data frame: columns will be truncated or padded with NAs in: format.data.frame(x, digits = digits)
Gregor GORJANC