On Feb 10, 2010, at 6:37 PM, Chaehan So wrote:

> Ah, you were right and it was my bad (my original code was a  
> dataframe) -
> but the main question remains:
>
> how can I access currentName in my loop - evaluated as a variable  
> although it is a string?
>
> createTable <- function(latentVariableNames)
> {
>       for (currentName in latentVariableNames)
>       {
>               doSomethingWith(currentName)
>       }
> }
>

 > a <- 3
 > b <- 4
 > clist <- list(a,b)
 > for (item in clist) item^2
 > for (item in clist) print(item^2)
[1] 9
[1] 16
 > cmtx <- vector()
 > for (idx in seq_along(clist) ) cmtx[idx] <- clist[[idx]]
 > cmtx
[1] 3 4

Two morals:
- that you may be able to use the items without resorting to get and  
assign if managed in an appropriate structure.
-that presenting R-defined examples will get you further than  
presenting pseudo-code.

-- 
David.

>
>
>
> On Wed, Feb 10, 2010 at 11:34 PM, David Winsemius <dwinsem...@comcast.net 
> > wrote:
>
> On Feb 10, 2010, at 5:12 PM, Chaehan So wrote:
>
> Thank you, it works for the first problem!
>
> Yet for the second problem, how can I solve that in one dataframe  
> (here:
> latentVariableNames),
> I store the names of the according dataframes as strings, and then  
> want to
> access the
> according dataframe (here: currentName) - see below?
>
> latentVariables <- c("Planning", "Testing", ...)
> createTable <- function(latentVariableNames)
> {
> for (currentName in names(latentVariableNames))
>
> ... but, but,  latentVariables is a name-less vector???? You didn't  
> give those strings any names.
>
> > latentVariables <- c("Planning", "Testing")
> > names(latentVariables)
> NULL
>
> If you want to iterate along a character vector, you use:
>
> for (item in vector) {
>              < body of loop working on item's >
>                     }
> OR;
>
> for (idx in seq_along(latentVariables) ) {
>         < operations on  latentVariables[idx] >
>             }
>
> (Review the basic manuals you are using to learn R.)
> -- 
> David
>
> {
> doSomethingWith(currentName)
> }
> }
>
>
> On Wed, Feb 10, 2010 at 8:36 PM, Henrique Dallazuanna <www...@gmail.com 
> >wrote:
>
> Try this:
>
> fileName <- "testfile%s"
> lapply(sprintf(fileName, 1:2), read.csv, dec = ",")
>
> On Wed, Feb 10, 2010 at 5:28 PM, Chaehan So <chaehan...@gmail.com>  
> wrote:
> Dear r-helpers,
>
> I am looking for an R-equivalent for the eval-function in javascript
> which
> can
> interpret a string as code on runtime, thereby allowing things like
>
> for (i in c(1:2))
> {
>
> eval(items + "i") <- read.csv(eval(filename+ i), dec=",");
>
> }
>
> which would execute (with filename="testfile"):
>
> items1 <- read.csv(testfile1, dec=",");
> items2 <- read.csv(testfile2, dec=",");
>
>
> The way I intend to use it is to define a string list containing  
> names of
> date.frames,
> and interate this list to perform some function:
>
> createTable <- function(latentVariableNames)
> {
> for (currentName in names(latentVariableNames))
> {
> doSomethingWith(currentName)
> }
> }
>
>
> Would be grateful for any help!
>
> Chaehan
>
>      [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@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.
>
>
>
>
> --
> Henrique Dallazuanna
> Curitiba-Paraná-Brasil
> 25° 25' 40" S 49° 16' 22" O
>
>
>
>
> -- 
> Humboldt University Berlin, Germany
> Institute of Psychology
> Rudower Chaussee 18, Room 1221
> 12489 Berlin
> Germany
> Office: +49 30 2093 - 9337
> Mobile: +49 171- 626 9373
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help@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.
>
> David Winsemius, MD
> Heritage Laboratories
> West Hartford, CT
>
>
>
>
> -- 
> Humboldt University Berlin, Germany
> Institute of Psychology
> Rudower Chaussee 18, Room 1221
> 12489 Berlin
> Germany
> Office: +49 30 2093 - 9337
> Mobile: +49 171- 626 9373
> Sent from Berlin, BE, Germany

David Winsemius, MD
Heritage Laboratories
West Hartford, CT


        [[alternative HTML version deleted]]

______________________________________________
R-help@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.

Reply via email to