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

Ivan Calandra ivan.calandra at uni-hamburg.de
Wed Feb 3 10:22:11 CET 2010


Hi!

Looks like get() doesn't work. Here is what I do and what I get (still 
in the loop):

>  names(get(paste(names(file)[3], name.num, sep="_")))<- levels(factor(file[[3]]))
Error in names(get(paste(names(file)[3], name.num, sep = "_")))<- levels(factor(file[[3]])) :
could not find function "get<-"

>  print(names(get(paste(names(file)[3], name.num, sep="_"))))  #4 iterations; I wanted to test this part
NULL
NULL
NULL
NULL


Does anyone has an idea about it?
Thanks
Ivan

Le 2/2/2010 19:38, Chris Campbell a écrit :
> On Tue, Feb 2, 2010 at 11:44, Ivan Calandra
> <ivan.calandra at uni-hamburg.de>  wrote:
>    
>> Hi David,
>>
>> Thanks for your answer.
>> But I don't really see how I can extend it with my real data.
>> The thing is that I have more than 3 names and 1 value for each name.
>> Moreover, each is different from one run to another. That is why I was
>> trying with a modification of names(). Also to be noted is that I simplified
>> the "name" in assign(); I actually have 2 other variables that will be
>> pasted to create the name.
>>
>> Here is my code (I kept only what is important for that part):
>> ----
>> library(WRS)
>> seq.num<- seq(7,10,1)        #column (variable) indexes to be used as
>> numerical variables
>>
>> # fac2list() separates the data from file[,k] into groups from levels in
>> file[3] and store into list mode.
>> for(i in 1:length(seq.num)) {
>>   k<- seq.num[i]
>>   name.num<- names(file)[k]
>>   assign(paste(names(file)[3], name.num, sep="_"), fac2list(file[,k],
>> file[3]))
>>   names(paste(names(file)[3], name.num, sep="_"))<-
>> levels(factor(file[[3]]))  #that line doesn't work, but I would like
>> something in this direction
>> }
>> ----
>>      
> Sounds like a job for 'get'.  Try this (untested):
>
> names(get(paste(names(file)[3], name.num, sep="_")))
>
> Good luck
>
>    
>> Thanks in advance for your help.
>> Regards,
>> Ivan
>>
>>
>>
>> Le 2/1/2010 18:47, David Winsemius a écrit :
>>      
>>> On Feb 1, 2010, at 12:33 PM, Ivan Calandra wrote:
>>>
>>>        
>>>> I have a follow-up question:
>>>>
>>>> I use assign() to store some value in my paste()-created object as
>>>> suggested:
>>>> for (i in 1:3) {
>>>> assign(paste("object", i, sep=""), c("a", "b", "c"))
>>>> }
>>>>
>>>> Then I would like to change the names of the elements of that object
>>>> within the loop. Since it is all in a loop, I cannot give the name of the
>>>> object manually by doing something like: names(object1)<- c("tooth",
>>>> "bone", "species").
>>>> The only thing I can give to names() is paste("object", i, sep=""), which
>>>> doesn't work.
>>>>
>>>> Any idea of how to do it?
>>>>          
>>>        
>>>> for (i in paste("object", 1:3, sep="")) {
>>>>          
>>> + assign(i, c("tooth"="a", "bone"="b", "species"="c") )
>>> + }
>>>        
>>>> object1
>>>>          
>>>   tooth    bone species
>>>     "a"     "b"     "c"
>>>
>>>        
>>>> Thanks in advance
>>>> Ivan
>>>>
>>>>
>>>> Le 2/1/2010 17:14, David Winsemius a écrit :
>>>>          
>>>>> Upon reading it yesterday, it appeared as it would have required some
>>>>> serious testing and there was no data on which to do any work.  You were
>>>>> clearly not taking the time to isolate the problem and construct a dataset.
>>>>> But who knows? When you say "What I want to do is. ... ,I would like the
>>>>> name of the list to be created in the loop too", maybe all you needed was to
>>>>> be pointed to was:
>>>>>
>>>>> ?assign
>>>>>
>>>>> But if that were the case, then you lost most of your audience along the
>>>>> way with a bunch of unneeded and obscure code.
>>>>>
>>>>>            
>>> David Winsemius, MD
>>> Heritage Laboratories
>>> West Hartford, CT
>>>
>>>
>>>        
>> ______________________________________________
>> 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.
>>
>>      
>    

-- 
Ivan CALANDRA
PhD Student
University of Hamburg
Biozentrum Grindel und Zoologisches Institut und Museum
Martin-Luther-King-Platz 3
D-20146 Hamburg, GERMANY
+49(0)40 42838 6231
ivan.calandra at uni-hamburg.de

**********
http://www.for771.uni-bonn.de
http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php



More information about the R-help mailing list