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

Gregor GORJANC gregor.gorjanc at bfro.uni-lj.si
Tue Feb 15 08:45:21 CET 2005


James, thanks for the response.

I understand now my puzzle.

tmp <- data.frame(y1=1:4, f1=factor(c("A", "B", "C", "D")))
tmp$y2 <- NA
tmp[1:2, "y2"] <- 2

Does the job. I see that I should add a full column. In my case adding full 
column of NAs and then adding values, solves my problems.

Thanks to all.

james.holtman at convergys.com wrote:
> 
> 
> 
> By definition, a data.frame has to have all columns of the same length
> (think of it as a matrix).  You can not add partial data to a column.  What
> is it you are trying to do?  Should you consider the use of a 'list' if you
> want each 'column' (data element) to have a different mode and length?
> __________________________________________________________
> James Holtman        "What is the problem you are trying to solve?"
> Executive Technical Consultant  --  Office of Technology, Convergys
> james.holtman at convergys.com
> +1 (513) 723-2929
> 
> 
>                                                                                                                                            
>                       Gregor GORJANC                                                                                                       
>                       <gregor.gorjanc at bfro.        To:       Prof Brian Ripley <ripley at stats.ox.ac.uk>, r-devel at stat.math.ethz.ch,         
>                       uni-lj.si>                    r-help at stat.math.ethz.ch                                                               
>                       Sent by:                     cc:                                                                                     
>                       r-help-bounces at stat.m        Subject:  [R] Re: [Rd] corrupt data frame: columns will be truncated or padded with NAs 
>                       ath.ethz.ch                   in: format.data.frame(x, digits = digits)                                              
>                                                                                                                                            
>                                                                                                                                            
>                       02/14/2005 07:32                                                                                                     
>                       Please respond to                                                                                                    
>                       gregor.gorjanc                                                                                                       
>                                                                                                                                            
> 
> 
> 
> 
> Hello!
> 
> Sending this also to r-help so anyone can read it also there and maybe also
> 
> help me with my puzzle if this trivial and I don't see it.
> 
> Prof Brian Ripley wrote:
> [... removed some ...]
> 
>>You add a column, not replace part of a non-existent column.  Isn't that
>>obvious, given what you wrote?
> 
> 
> # OK. If I do
> tmp <- data.frame(y1=1:4, f1=factor(c("A", "B", "C", "D")))
> tmp[1:2, "y2"] <- 2
> tmp
> # I am changing nonexistent column y2 in data frame tmp.
> 
> # If I do
> tmp <- data.frame(y1=1:4, f1=factor(c("A", "B", "C", "D")))
> tmp$y2 <- NA
> tmp[1:2, "y2"] <- 2
> tmp
> # I am changing existent column. I understand now the difference. However,
> # it is weird for me that this is OK (if column y2 does not yet exist)
> tmp["y2"] <- 2
> # but this is not
> tmp[1:2, "y2"] <- 2
> 
> 
>>There is a lot of basic documentation on data manipulation in R/S, and a
>>whole chapter in MASS4.  Somehow most other people don't seem to find
>>this a problem.
> 
> 
> I just ordered MASS4 last week and I am eager to get it in my hands. In
> meanwhile I read quite some documentation and what I more or less saw is
> 
> tmp <- data.frame(y1=1:4, f1=factor(c("A", "B", "C", "D")))
> tmp$y2 <- 1:4
> tmp$y3 <- 2*tmp$y1
> ...
> ...
> 
> i.e. everybody is adding full column to data frame. But I would like to add
> 
> just one part.
> 
> --
> 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
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide!
> http://www.R-project.org/posting-guide.html
> 
> 
> 

-- 
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




More information about the R-help mailing list