Re: [R] Transforming a list to a vector with associated levels

From: David Winsemius <dwinsemius_at_comcast.net>
Date: Tue, 18 Nov 2008 23:45:25 -0500

My guess is that you are dimly remembering stack(.)

t <- list(A=c(4,1,4),B=c(3,7,9,2))

 > stack(t)
   values ind

1      4   A
2      1   A
3      4   A
4      3   B
5      7   B
6      9   B
7      2   B

If you needed the internal factor of ind that is possible as well

 > as.numeric(stack(t)$ind)
[1] 1 1 1 2 2 2 2

so
data.frame(t=stack(t)$values, levels=as.numeric(stack(t)$ind))

--
David Winsemius

On Nov 18, 2008, at 10:15 PM, Blanchette, Marco wrote:

> I am pretty sure that I came across a function that creates a vector
> of levels from a list but I just can't remember.
>
> Basically, I have something like
>
>> t <- list(A=c(4,1,4),B=c(3,7,9,2))
>> t
> $A
> [1] 4 1 4
>
> $B
> [1] 3 7 9 2
>
> And I would like to get something like the following:
> t levels
> 4 1
> 1 1
> 4 1
> 3 2
> 7 2
> 9 2
> 2 2
>
> I tried unlist without success. I also do remember that there is a
> corresponding function that create a list of t's according to the
> level from the matrix a draw but no luck to remember it.
>
> Thanks
>
> --
> Marco Blanchette, Ph.D.
> Assistant Investigator
> Stowers Institute for Medical Research
> 1000 East 50th St.
>
> Kansas City, MO 64110
>
> Tel: 816-926-4071
> Cell: 816-726-8419
> Fax: 816-926-2018
>
> ______________________________________________
> R-help_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



R-help_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Wed 19 Nov 2008 - 04:54:53 GMT

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Wed 19 Nov 2008 - 05:30:26 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive