[R] data.frame contents are not displayed

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri May 16 13:08:10 CEST 2003


$<- is not the right way to change the contents of a data frame.
You have updated the underlying list, but you haven't (for example) added 
any row names, and you have circumvented the checks that all columns 
should have the same length (and they do not).

If you had created a data frame of the right size, then $<- would have 
replaced the contents of the column -- that's the only safe way to use it.
If you used 

> data7902 <- data.frame(year=NULL, lon=NULL)
> data7902
NULL data frame with 0 rows
> names(data7902)
character(0)

you will see that you don't even have any columns.

On Fri, 16 May 2003, Wladimir Eremeev wrote:

> Dear r-help,
> 
> I need to unite contents of two files with variables into one data
> frame, produce some derivative variables from them and store them in
> that data frame.
> 
> Here is the set of commands (processing first file for now)
> 
> data7902<-data.frame(year=NULL,lon=NULL,area=NULL,extent=NULL,area.std=NULL,extent.std=NULL,
>                      area.norm=NULL,extent.norm=NULL);
> 
> area7902<-read.table("a7902-15.txt",
>                      col.names=c("year","area","lon"),
>                      header=FALSE,
>                      comment.char='#');
> 
> data7902$year<-area7902$year;
> data7902$area<-area7902$area;
> data7902$lon<-area7902$lon;
>   
> Checking:
> I type "area7902" in the R console and get
> 
>     year         area lon
> 1   1979  78164.77944   0
> 2   1979  65492.49919  20
> 3   1979  41491.50425  40
> 4   1979  15217.32476  60
> ....
> 
> Then I type "data7902" in the R console.
> 
> [1] year area lon
> <0 rows> (or 0-length row.names)
> 
> For the same time typing "data7902$year" gives the list of years for
> all rows in data frame.
> 
> Why contents of the newly created and filled data frame are not
> displayed?
> 
> 

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list