R-alpha: list assignment

Peter Dalgaard BSA p.dalgaard@kubism.ku.dk
25 Apr 1997 13:09:01 +0200


Robert Gentleman <rgentlem@stat.auckland.ac.nz> writes:

> 
> 
> Peter,
>   They don't work on purpose. Basically this is the same problem as indexing
> beyond the end of an array. In almost all cases it is an error.
> To add a variable with a specific name to a data frame how about
> 
> x (the dataframe)
> var1 the variable
> name1 the name
> 
>  x<-cbind(x,var1)
>  names(x)[ncol(x)]<-name1

Hm. Not sure about that argument. After all we don't protect against
typing

x$mumble<-17
instead of 
x$grumble<-17

Once you're into indexing lists through character variables, shouldn't
you be assumed to know what you're doing? Beginners can do much more
mischief with e.g. dfr$new<-0 which will turn dfr into a list.

However, apropos your construct, watch this:

> x<-list()
> x[[1]]<-23
> names(x)[1]<-"a"
> x
$"a"
[1] 23

> unlist(x)
\"a\" 
   23 
> names(x)[1]     
[1] "\"a\""
> names(x)   
[1] "\"a\""
> names(x)[1]<-"a"
> names(x)
[1] "a"
> x
$a
[1] 23

> unlist(x)
 a 
23 


-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk)             FAX: (+45) 35327907
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-