[R] Lists

jim holtman jholtman at gmail.com
Sat Jul 25 10:27:45 CEST 2009


Here is one way:

> a<-c("uno","dos","tres")
> x <- list()
> a<-c("uno","dos","tres")
> x <- list()
> for (i in seq_along(a)){
+   # add to the list
+   x[[i]] <- a[i]
+   str(x)
+ }
List of 1
 $ : chr "uno"
List of 2
 $ : chr "uno"
 $ : chr "dos"
List of 3
 $ : chr "uno"
 $ : chr "dos"
 $ : chr "tres"
>
> x
[[1]]
[1] "uno"

[[2]]
[1] "dos"

[[3]]
[1] "tres"

An easier way to get the same result is:

> as.list(a)
[[1]]
[1] "uno"

[[2]]
[1] "dos"

[[3]]
[1] "tres"



On Fri, Jul 24, 2009 at 6:48 PM, Alberto Lora M<albertoloram at gmail.com> wrote:
> Hi Everybody
>
> I have the following problem
>
> suppose that we
>
> a<-c("uno","dos","tres")
>
> I am working with a while cycle and the idea is in each iteration adding an
> item to a list
>
> In the first iteration the resultshould be:
> [[1]]
> [1] "uno"
>
> In the second
> [[1]]
> [1] "uno"
> [[2]]
> [1] "dos"
>
> And the final result
> [[1]]
> [1] "uno"
> [[2]]
> [1] "dos"
> [[3]]
> [1] "tres"
>
> How can I do that
> Thx again
> --
> Alberto
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> 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.
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?




More information about the R-help mailing list