Skip to content

a simple list question

8 messages · Erin Hodgess, Rolf Turner, R. Michael Weylandt +4 more

#
Dear R People

I have a simple list question, please:

I have vectors x.1, x.2,...x.n (each of different lengths) and I would
like to combine them into a list.

However, I'm sure that there is a better way to do this than to type
in x <- list(x.1,x.2,x.3,...)


Is there a better way to do this, please?  I was thinking about
possibly using grep?

Thanks,
Sincerely,
Erin
#
On 07/11/12 18:35, Erin Hodgess wrote:
If I understand your question correctly --- always a dubious assumption ---
you could do:

     n <- <however many vectors you have>
     xlist <- lapply(1:n,function(n){get(paste("x",n,sep="."))})

Does that accomplish your goal?

     cheers,

         Rolf
#
On Nov 7, 2012, at 6:07 AM, Rolf Turner <rolf.turner at xtra.co.nz> wrote:

            
Perhaps an hair shorter with mget()

Michael
#
Hello,

Try the following.

x.1 <- 1:3
y.1 <- 1:4
x.2 <- 5:10


vecs <- ls()[grep("^x\\.[[:digit:]]+$", ls())]
lapply(vecs, function(.x) assign(.x, get(.x)))


Then you can use vecs to attribute names() to the result.

Hope this helps,

Rui Barradas

Em 07-11-2012 05:35, Erin Hodgess escreveu:
#
HI,


If you want to combine vectors? "x" and "y" into a list,
may be this also helps:
x.1<-1:3
x.2<-1:4
x.3<-5:10
y.1<-5:6
y.2<-8:10
ls()
# [1] "a"??? "a1"?? "L"??? "mat1" "n"??? "vecs" "x.1"? "x.2"? "x.3"? "y.1" 
#[11] "y.2" 

lapply(strsplit(ls()[grep("x|y",ls())],""),function(x) get(paste(x,collapse="")))
#[[1]]
#[1] 1 2 3
#
#[[2]]
#[1] 1 2 3 4
#
#[[3]]
#[1]? 5? 6? 7? 8? 9 10
#
#[[4]]
#[1] 5 6
#
#[[5]]
#[1]? 8? 9 10
A.K.




----- Original Message -----
From: Rui Barradas <ruipbarradas at sapo.pt>
To: Erin Hodgess <erinm.hodgess at gmail.com>
Cc: R help <r-help at stat.math.ethz.ch>
Sent: Wednesday, November 7, 2012 6:44 AM
Subject: Re: [R] a simple list question

Hello,

Try the following.

x.1 <- 1:3
y.1 <- 1:4
x.2 <- 5:10


vecs <- ls()[grep("^x\\.[[:digit:]]+$", ls())]
lapply(vecs, function(.x) assign(.x, get(.x)))


Then you can use vecs to attribute names() to the result.

Hope this helps,

Rui Barradas

Em 07-11-2012 05:35, Erin Hodgess escreveu:
______________________________________________
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.
#
Hello,

My solution is too complicated, the assign() is not needed.


lapply( ls()[grep("^x\\.[[:digit:]]+$", ls())], get)


Hope this helps,

Rui Barradas
Em 07-11-2012 11:44, Rui Barradas escreveu:
#
Combining your, Rolf, and Michael's suggestions makes it possible 
to eliminate the lappy():

mget(ls()[grep("^x\\.[[:digit:]]+$", ls())], .GlobalEnv)

----------------------------------------------
David L Carlson
Associate Professor of Anthropology
Texas A&M University
College Station, TX 77843-4352
#
And the
   ls()[grep("^x\\.[[:digit:]]+$", ls())]
can be simplified to
   grep("^x\\.[[:digit:]]+$", ls(), value=TRUE)

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com