R-alpha: list assignment

Kurt Hornik Kurt.Hornik@ci.tuwien.ac.at
Mon, 28 Apr 1997 07:07:05 +0200


>>>>> Peter Dalgaard BSA writes:

> 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

Which is the same (?) as the

R> l <- list("11" = 1:5)
R> l
$11
[1] 1 2 3 4 5

R> unlist(l)
111 112 113 114 115 
  1   2   3   4   5 

I ran into this weekend ...

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