[R] writing function : can't find an object

Ivan Calandra ivan.calandra at uni-hamburg.de
Wed May 26 09:41:22 CEST 2010


Hi,

The first problem (I think) is your for loop: for (i in sel), what is 
"sel"?!
Then you might want to write "position[i]" and "trade[i]" (I don't think 
that position and trade are functions, or they are in a package you 
don't specify). Depending on the class of position and trade, you might 
need position[[i]] and trade[[i]]

I didn't really go further. The only problem I have is why only PosB? If 
I'm right, PosA shouldn't work either.

In any case, you have to find by yourself where exactly is the problem. 
You can run line by line by simulating the loop yourself (replacing i 
with 1, then with 2 and so on) to see if what happens at each place is 
really what you want. It might be a cheap solution, but it does really 
help me to make sure that it does what I need.

Once you've found the problem, and if you cannot resolve it, then 
contact the list, it will be much easier for anyone to help you if the 
problem is identified than sending a long code without even the sample 
data to test it.

HTH,
Ivan

Le 5/26/2010 09:17, arnaud Gaboury a écrit :
> Dear group,
>
> Here is my function:
>
>
> #return the daily PL for day y
>
> PLDaily<-function(x,y)
>
>
> {
>
> #find elements in my directory with "LSCPos" in the name, keep the numeric
> part in the name and
> #create a list
>    l<-gsub("\\D","",dir()[grep("LSCPos",dir())])
>
> #select in the list the desired elements
>    assign("sel",l[which(l==x):which(l==y)],envir=.GlobalEnv)
>    #here is another solution  select<-l[l %in% seq(x, y)]
>
> #first we need to create the Pos and Trad elements
>
>    for (i in sel)  {
>
>    assign(paste("Pos",i,sep=""),position(i),envir=.GlobalEnv)
>    assign(paste("Trad",i,sep=""),trade(i),envir=.GlobalEnv)
>
>                     }
> #access elements in my environment
>    posA<-get(paste(c("Pos",x),collapse=""))
>    posB<-get(paste(c("Pos",y),collapse=""))
>    av<-get(paste(c("Trad",y),collapse=""))
>
> #apply some change on element columns then create only one data frame with
> the three elements
>    allcon<-ddply(rbind(av[,1:3],
> transform(posA,prix=POSITION*SETTLEMENT,SETTLEMENT=NULL),
>            transform(posB, prix = -POSITION * SETTLEMENT, SETTLEMENT = NULL,
> POSITION = POSITION * -1)),
>            "DESCRIPTION",summarise,pl=sum(prix),quantity=sum(POSITION))
>
> #remove the date in $DESCRPTION and add a new column $SHORTDESCRIPTION
>    allcon$SHORTDESCRIPTION<-sub('
> [a-z]{3}/[0-9]{2}','',allcon$DESCRIPTION,ignore.case=TRUE)
>
> #read the contractvalue file
>    value<-read.csv2("contractvalue.csv",sep=",",h=T,strip.white=T)
>
> #merge "value" with "allcon", change some columns, then merge with PosB,
> replace NA by zero and assign the final result to element PL
>    zz<-merge(transform(merge(value,allcon,all.y=T),SHORTDESCRIPTION=NULL,
>        VALUE=NULL,PL=-VALUE*pl,quantity=NULL),PosB,all.x=T,sort=F)
>    zz[is.na(zz)]<-0
>    #PL<-zz[c(1,3,4)]
>    assign(paste("DailyPL",y,sep=""),zz[,c(1,3,4)],envir=.GlobalEnv)
>
> }
>
> Here is what I get :
>
>    
>> PLDaily(100524,100525)
>>      
> Error in as.data.frame(y) : object 'PosB' not found
>
>    
>> ls()
>>      
> [1] "PLDaily"    "Pos100524"  "Pos100525"  "position"   "sel"
> "Trad100524" "Trad100525" "trade"
>
> Why R can't find "PosB" ?
>
> TY for your help.
>
> ______________________________________________
> 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 Museum
Abt. Säugetiere
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