I keep finding myself in a situation where I want to calculate a variable name and then use it on the left hand side of an assignment. For example iteration <- 1 varName <- paste("run",iteration,sep="") myList$parse(text=varName) <- aColumn I want to take some existing variable "aColumn" and use the name "varName" name for it and put it into a list "myList". That use fails with this error: Error: couldn't find function "(<-" I've tried many variations on the theme. If I could do this with a data frame, I would be just as happy. Right now I'm using a list rather than a data frame because not all columns are of the same length. But I can work around that. -- Paul E. Johnson email: pauljohn at ukans.edu Dept. of Political Science http://lark.cc.ukans.edu/~pauljohn University of Kansas Office: (785) 864-9086 Lawrence, Kansas 66045 FAX: (785) 864-5700
Paul E. Johnson wrote:> I keep finding myself in a situation where I want to calculate a > variable name and then use it on the left hand side of an assignment. > For example > > iteration <- 1 > varName <- paste("run",iteration,sep="") > myList$parse(text=varName) <- aColumn > > I want to take some existing variable "aColumn" and use the name > "varName" name for it and put it into a list "myList". That use fails > with this error: > > Error: couldn't find function "(<-" > > I've tried many variations on the theme. > > If I could do this with a data frame, I would be just as happy. Right > now I'm using a list rather than a data frame because not all columns > are of the same length. But I can work around that. >See ?assign. Uwe Ligges
Have you considered the following: > myList <- list() > iteration <- 1 > varName <- paste("run",iteration,sep="") > myList[[varName]] <- "aColumn" > myList $run1 [1] "aColumn" hth. spencer graves Paul E. Johnson wrote:> I keep finding myself in a situation where I want to calculate a > variable name and then use it on the left hand side of an assignment. > For example > > iteration <- 1 > varName <- paste("run",iteration,sep="") > myList$parse(text=varName) <- aColumn > > I want to take some existing variable "aColumn" and use the name > "varName" name for it and put it into a list "myList". That use fails > with this error: > > Error: couldn't find function "(<-" > > I've tried many variations on the theme. > > If I could do this with a data frame, I would be just as happy. Right > now I'm using a list rather than a data frame because not all columns > are of the same length. But I can work around that. >
At 11:57 AM 5/31/2003 -0500, Paul E. Johnson wrote:>I keep finding myself in a situation where I want to calculate a variable >name and then use it on the left hand side of an assignment. For example > >iteration <- 1 >varName <- paste("run",iteration,sep="") >myList$parse(text=varName) <- aColumn > >I want to take some existing variable "aColumn" and use the name >"varName" name for it and put it into a list "myList". That use fails >with this error: > >Error: couldn't find function "(<-" > >I've tried many variations on the theme. > >If I could do this with a data frame, I would be just as happy. Right now >I'm using a list rather than a data frame because not all columns are of >the same length. But I can work around that.Dear Paul, I don't think that assign() will work in this context, but I may be wrong. One way to proceed is with eval() and parse(), assembling the command to be executed as a text string; for example, eval(parse(text=paste("myList$", varName, " <- aColumn", sep=""))), though you do have to be careful about the environment in which the expression is evaluated -- see the envir argument to eval. I hope that this helps, John ----------------------------------------------------- John Fox Department of Sociology McMaster University Hamilton, Ontario, Canada L8S 4M4 email: jfox at mcmaster.ca phone: 905-525-9140x23604 web: www.socsci.mcmaster.ca/jfox
On Sat, 31 May 2003, Paul E. Johnson wrote:> I keep finding myself in a situation where I want to calculate a > variable name and then use it on the left hand side of an assignment. > For example > > iteration <- 1 > varName <- paste("run",iteration,sep="") > myList$parse(text=varName) <- aColumn > > I want to take some existing variable "aColumn" and use the name > "varName" name for it and put it into a list "myList". That use fails > with this error: > > Error: couldn't find function "(<-" > > I've tried many variations on the theme. > > If I could do this with a data frame, I would be just as happy. Right > now I'm using a list rather than a data frame because not all columns > are of the same length. But I can work around that.For a data frame you could use mydf[paste("run",iteration,sep="")] <- aColumn and for a list or a data frame Robject[[paste("run",iteration,sep="")]] <- aColumn -- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
On Sat, 31 May 2003, Paul E. Johnson wrote:> I keep finding myself in a situation where I want to calculate a > variable name and then use it on the left hand side of an assignment. > For example > > iteration <- 1 > varName <- paste("run",iteration,sep="") > myList$parse(text=varName) <- aColumn >In this particular case you can just use myList[[varName]]<-aColumn. I don't think you can (easily) use assign, despite the suggestion -- it doesn't dispatch assignment methods. If you wanted to do something of this sort for which the above didn't work you could also learn about substitute() eval(substitute(myList$newColumn<-aColumn), list(newColumn=as.name(varName))) as an alternative to parse(). -thomas