Dear R People: I have several vectors, sa1, sa2,...sa27 of varying lengths. I want to produce one vector xener[1:27] which has the minimum of each sa[i]. I'm trying to set up a loop and use the assign statement, but here are my results:> for(i in 1:27) {+ xener[i] <- min(assign(paste("sa",i,sep=""))) + } Error in assign(paste("sa", i, sep = "")) : element 2 is empty; the part of the args list of '.Internal' being evaluated was: (x, value, envir, inherits)>Any suggestions would be most welcome. Thanks in advance, Erin -- Erin Hodgess Associate Professor Department of Computer and Mathematical Sciences University of Houston - Downtown mailto: erinm.hodgess at gmail.com
How about: sapply ( 1:27 , function ( i ) { min ( get ( paste ( "sa" , i , sep = "" ) ) ) } ) See ?get david -- David ? ----------------------------------------------------- David Huffer, Ph.D. Senior Statistician CSOSA/Washington, DC david.huffer at csosa.gov ----------------------------------------------------- -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of Erin Hodgess Sent: Monday, July 20, 2009 2:26 PM To: R help Subject: [R] assign question Dear R People: I have several vectors, sa1, sa2,...sa27 of varying lengths. I want to produce one vector xener[1:27] which has the minimum of each sa[i]. I'm trying to set up a loop and use the assign statement, but here are my results:> for(i in 1:27) {+ xener[i] <- min(assign(paste("sa",i,sep=""))) + } Error in assign(paste("sa", i, sep = "")) : element 2 is empty; the part of the args list of '.Internal' being evaluated was: (x, value, envir, inherits)>Any suggestions would be most welcome. Thanks in advance, Erin -- Erin Hodgess Associate Professor Department of Computer and Mathematical Sciences University of Houston - Downtown mailto: erinm.hodgess at gmail.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 don't think you want assign() here.> x1 = rnorm(20) > min(x1)[1] -0.9723398> min(eval(paste("x",1,sep=""))) # not the solution[1] "x1"> min(eval(as.name(paste("x",1,sep="")))) # a solution[1] -0.9723398 try: for(i in 1:27) { xener[i] <- min(eval(as.name((paste("sa",i,sep=""))))) } albyn On Mon, Jul 20, 2009 at 01:26:05PM -0500, Erin Hodgess wrote:> Dear R People: > > I have several vectors, sa1, sa2,...sa27 of varying lengths. > > I want to produce one vector xener[1:27] which has the minimum of each sa[i]. > > I'm trying to set up a loop and use the assign statement, but here are > my results: > > > for(i in 1:27) { > + xener[i] <- min(assign(paste("sa",i,sep=""))) > + } > Error in assign(paste("sa", i, sep = "")) : > element 2 is empty; > the part of the args list of '.Internal' being evaluated was: > (x, value, envir, inherits) > > > > Any suggestions would be most welcome. > > Thanks in advance, > Erin > > > -- > Erin Hodgess > Associate Professor > Department of Computer and Mathematical Sciences > University of Houston - Downtown > mailto: erinm.hodgess at gmail.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. >