[R] create an object in a loop (v2)

Ivan Calandra ivan.calandra at uni-hamburg.de
Mon Feb 1 16:45:16 CET 2010


Hi everybody,

I'm kind of surprised that I didn't get any answer to my question... Is 
it not interesting? Is it not well explained? Am I doing something 
wrong? Was it lost in the hundreds of other emails during the weekend?

I hope it is because of my last guess. So I'll just send the email again 
(see below).
I really need some help on this matter.

Thanks in advance.

> To run some statistical tests from the package WRS (from Rand R 
> Wilcox), I need to store my data in a list, which fac2list() from this 
> package does very well.
> But I would like to do it in a loop for each numerical variable. It 
> would be easier!
>
> For now, I have the loop with the extraction and storage into a list. 
> The code is below.
> -----
> # Defines variables
> file <- ssfamed                          #data file object to be used
> spec <- "cotau"                         #species selection
> bone <- "tx"                              #tooth row selection
> cat <- 3                                    #column indexe to be used 
> as categorical variable
> seq.num <- c(seq(7,21,1))        #column indexes to be used as 
> numerical variables
>
> # Select data for species and tooth row
> select <- file[file$SPECSHOR==spec & file$BONE==bone, ]
>
> #Separate the data from each numeric variable in seq.num (select[,k]) 
> into groups from levels in select[cat] and store into list mode.
> for(i in 1:length(seq.num)) {
>  k <- seq.num[i]
>  name.num <- names(file)[k]
>  TO_POS_Asfc.median_cotautx <- fac2list(select[,k], select[cat])
>  names(TO_POS_Asfc.median_cotautx) <- levels(factor(select[[cat]]))
> }
> -----
>
> What I want to do is, instead of giving manually the name of the list 
> (here TO_POS_Asfc.median_cotautx), I would like the name of the list 
> to be created in the loop too, so that it looks like: 
> select[[cat]]_name.num_specbone. In the code it would look like:
> -----
> select[[cat]]_name.num_specbone <- fac2list(select[,k], select[cat])  
> #create the list from the values of these variables at each iteration 
> of the loop
> names(select[[cat]]_name.num_specbone) <- levels(factor(select[[cat]]))
> -----
> I thought about using paste(), but I cannot create an object like this.
> In my small R life, I've always stored data into pre-defined objects, 
> I have no idea how to create an object automatically within a loop.
>
> Is my question clear? I hope so. I thought there might be a function 
> that would create an object where the name would come from a string 
> stored in an other object.
>
> Why do I want that? Simply because I want to run several statistical 
> tests on each list and I would therefore like to have standardized 
> names for the data objects.
>
> There might be easier ways to do what I want to, I'm open to all 
> suggestions but I would prefer to stay close from my idea (I would 
> understand better!)



More information about the R-help mailing list