[R] convert 'list' to 'vector'?

David Winsemius dwinsemius at comcast.net
Wed Aug 10 23:39:38 CEST 2011


On Aug 10, 2011, at 3:58 PM, Liviu Andronic wrote:

> On Wed, Aug 10, 2011 at 9:32 PM, David Winsemius <dwinsemius at comcast.net 
> > wrote:
>>> Thanks all! This is perfect, and very R-ish: never where a novice
>>> would expect it to be.
>>
>> Well, since `unlist` is linked in the See Also on the help page for  
>> `list`,
>> I can only hope you meant that in complete jest.
>>
> More or less. I would have expected that to transform a 'list' into a
> 'vector' I should look into 'as.vector' (or its See Also), and I would
> have never guessed to look for 'unlist'.

I am not sure if ?as.vector would necessarily be the place one would  
have wanted to stay after reading that vector() could return a list. I  
would also think that attempting this on your proffered data example  
would have made you want to search elsewhere:

 > is.vector(x)
[1] TRUE

You probably didn't realize that lists _are_ vectors before this.

>
> R documentation is sometimes (often?) hard to parse, and when learning
> R more often than not you're looking in the wrong place. But yes, it
> was intended as humour (although I did expect to get grilled).

Hopefully this expected invitation to grilling will not result in  
severe burns. I note that one of the *apply family would have been  
successful in returning the desired object in conjunction with c() :

 > rapply(x, c)
  [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l"

`rapply` is designed to do a complete traversal of the list structure  
and its default "how" argument is "unlist". It was linked from the  
page where you were looking at `simplify2array`.

>> As the Posting Guide says: "... sometimes `read the manual' is the
>> appropriate response".
>>
> I did, but I was on the wrong track. It actually hasn't occurred to me
> to check ?list, but See Also in both ?as.vector and ?simplify2array
> does not link to 'unlist'.

Perhaps not immediately but after on hop it does. The link to is.list  
on ?as.vector goes to the help(list) page.


> Since these are the two places where I
> turned to in the first place, and I have also played extensively with
> sapply(..., simplify=...) arguments, and there was nothing obvious in
> their respective See Also, I figured that I did my homework reasonably
> well.

I take your meaning. Learning to keep straight modes straight (atomic  
versus recursive) and the various classes is part of the "learning  
cliff".

>
> Regards
> Liviu

David Winsemius, MD
West Hartford, CT



More information about the R-help mailing list