[R] Odp: create an object in a loop (v2)
Petr PIKAL
petr.pikal at precheza.cz
Mon Feb 1 17:09:01 CET 2010
Hi
I would recommend to create a dummy object of class list with names
vvv<-vector("list", 5)
> vvv
[[1]]
NULL
[[2]]
NULL
[[3]]
NULL
...
names(vvv)<-paste(letters[1:5], "_", 1:5, "_some.thing", sep="")
vvv
$a_1_some.thing
NULL
$b_2_some.thing
NULL
$c_3_some.thing
NULL
...
Than you can call your list according to name or according to number
for (i in 1:5) vvv[[i]] <- some.procedure
But I am not sure if it suits your purpose.
Regards
Petr
r-help-bounces at r-project.org napsal dne 01.02.2010 16:45:16:
> 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!)
>
> ______________________________________________
> 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.
More information about the R-help
mailing list