[R] ls() pattern

Moshe Olshansky m_olshansky at yahoo.com
Thu Dec 13 23:59:10 CET 2007


Hi Livia,

Below is my solution. I believe that other list
members will post a better one.

Regards,

Moshe.

> rm(list=ls())
>
eval(parse(text=paste(paste("x",1:5,sep=""),paste("<-1.",1:5,";",sep=""))))
>
eval(parse(text=paste(paste("y",1:5,sep=""),paste("<-2.",1:5,";",sep=""))))
>
eval(parse(text=paste(paste("xy",1:5,sep=""),paste("<-12.",1:5,";",sep=""))))
> ls()
 [1] "x1"  "x2"  "x3"  "x4"  "x5"  "xy1" "xy2" "xy3"
"xy4" "xy5" "y1"  "y2"  "y3"  "y4"  "y5" 
> ls(pattern="xy")
[1] "xy1" "xy2" "xy3" "xy4" "xy5"
> a <- ls(pattern="xy")
> b <-
paste("df<-data.frame(",paste(a,"=",a,",",sep="",collapse=""),")",sep="")
> b
[1]
"df<-data.frame(xy1=xy1,xy2=xy2,xy3=xy3,xy4=xy4,xy5=xy5,)"
> substr(b,nchar(b)-1,nchar(b)-1) <- " "
> b
[1]
"df<-data.frame(xy1=xy1,xy2=xy2,xy3=xy3,xy4=xy4,xy5=xy5
)"
> eval(parse(text=b))
> df
   xy1  xy2  xy3  xy4  xy5
1 12.1 12.2 12.3 12.4 12.5
> 


--- livia <yn19832 at msn.com> wrote:

> 
> Hello everyone,
> 
> I get some data in the following format and I would
> like to combine them to
> form a dataframe. 
> 
> The data is like:
> 
> cbcname1 = 0.1,
> cbcname2= 0.2,
> cbcname3=0.3,...
> 
> name1, name2, name2 are just some random names. I
> would like to achieve sth
> like:
> (cbcname1=0.1, cbcname2=0.2, cbcname3=0.3,......)
> 
> I am using the following codes
> do.call(cbind, ls(pat=paste("cbc",*,"=")))
> 
> But it seems like the ls() part does not work. 
> 
> Could anyone give me some advice? Many thanks.
> 
> 
> -- 
> View this message in context:
>
http://www.nabble.com/ls%28%29-pattern-tp14316765p14316765.html
> Sent from the R help mailing list archive at
> Nabble.com.
> 
> ______________________________________________
> 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.
>



More information about the R-help mailing list