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.
> I get some data in the following format and I would like to combine themto> 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 achievesth> like: > (cbcname1=0.1, cbcname2=0.2, cbcname3=0.3,......) > > I am using the following codes > do.call(cbind, ls(pat=paste("cbc",*,"=")))Try this: unlist(mget(ls(pattern="cbc"), envir=.GlobalEnv)) The output is an array rather than a data frame, but so would be the output from cbind. Regards, Richie. Mathematical Sciences Unit HSL ------------------------------------------------------------------------ ATTENTION: This message contains privileged and confidential inform...{{dropped:20}}
> Thank you very much for your reply.I came across with another > problem for assigning the variables. > in order to achieve cbcname1 = 0.1, I would use sth like: > > paste("cbc","name1","=",function(),sep="") > > But it seems incorrect. Any advice?This sounds like a different problem. Do you mean that you want to automatically generate variable names and assign something to the resultant strings? May I suggest you read the FAQ on R, 7.21 "How can I turn a string into a variable?" Regards, Richie. Mathematical Sciences Unit HSL ------------------------------------------------------------------------ ATTENTION: This message contains privileged and confidential inform...{{dropped:20}}
> cbcname1 <- rnorm(100,0,1) > cbcname2 <- rnorm(100,0.5,1) > y <- rnorm(100,0.6,1) > lm1 <- lm(y~cbcname1+cbcname2) > > I have finished the linear regression until here. Now I would > like to predict for new data. This is the question part. > > data <- data.frame(cbcname1=0.1, cbcname2=0.2) > predict(lm1,data)This works fine for me. Regards, Richie. Mathematical Sciences Unit HSL ------------------------------------------------------------------------ ATTENTION: This message contains privileged and confidential inform...{{dropped:20}}
> Yes, I know this works. I just give it for example to show what I > would like to achieve. In fact, I have many variables, you can > imagine it is very annoying to construct the data frame by mannually > assigning the variables and binding them. Therefore, I would like to > do it automaticlly. So it is back to my first two questions, In > order to achieve cbcname1 = 0.1, I tried sth like:(but it does not > return the result as desired) > paste("cbc","name1","=",0.1,sep=""), in this way, I can construct > all the variables by a single step.Then I would like to build the > data frame automaticlly binding all the "cbcname=.." together. Just > like the example.Ok. How's this? #Generate variable names varnames = paste("cbcname", 1:10, sep="") #Assign them to values for(i in 1:10) assign(varnames[i], 0.1*i) #Form the middle bit of the command cmd = paste(varnames, "=", mget(varnames, envir=.GlobalEnv), collapse=",") #Evaluate the command eval(parse(text=paste("df=data.frame(", cmd, ")", sep=""))) There's a general rule that says if you have to use parse, you're probably doing it the wrong way, but it's been a long day, so that's the best way I can think of right now. Regards, Richie. Mathematical Sciences Unit HSL ------------------------------------------------------------------------ ATTENTION: This message contains privileged and confidential inform...{{dropped:20}}
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)) > dfxy1 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. >
> I am sorry I have to bother you again. I am trying your conmmands > and until the "cmd" part, it looks fine. > So I call "cmd", it gives the following output: > > [1] "cbcDummy10to12 = 1,cbcForeWrld_Ret = 0.04,cbcYC10-2_wld = 64, > cbcEP_us = 0.5,cbcDelta102YC_wld = 8,cbcGold = 2" > > But after that, if I call: eval(parse(text=paste("df=data. > frame(", cmd, ")", sep=""))) > > It says: Error in parse(file, n, text, prompt, srcfile, encoding) : > syntax error, unexpected $undefined, expecting ',' in "df=data. > frame(cbcDummy10to12 = 1,cbcForeWrld_Ret = 0.04,cbcYC10-2_"cbcYC10-2_wld is an invalid variable name, since it contains a minus sign. Regards, Richie. Mathematical Sciences Unit HSL ------------------------------------------------------------------------ ATTENTION: This message contains privileged and confidential inform...{{dropped:20}}