[R] save a data frame in environment

Uwe Ligges ligges at statistik.tu-dortmund.de
Tue Apr 27 12:14:47 CEST 2010



On 27.04.2010 12:13, arnaud Gaboury wrote:
> If I add these 2 lines in my function :
>
> Return(PositionFut)
> Return(PositionLme)
>
> Then, once the function is ran, when I do a ls(), I still don't see these 2
> objects.


Oh, come on, can't you read basic documentation about R and ?return?

You can only return *1* object, hence you need to return a list of the 
two data.frames, as I said,

moreover, you need to assign the value fromyour function call to some 
object name in the environemnt you call your function from.

Uwe Ligges


>> -----Original Message-----
>> From: Uwe Ligges [mailto:ligges at statistik.tu-dortmund.de]
>> Sent: Tuesday, April 27, 2010 11:44 AM
>> To: arnaud Gaboury
>> Cc: r-help at r-project.org
>> Subject: Re: [R] save a data frame in environment
>>
>> Don't print them but return() them from your function position() within
>> a list. Then you will have a list of two data.frames for later usage
>> assigned to your list.
>>
>>
>> Uwe Ligges
>>
>>
>> On 27.04.2010 11:37, arnaud Gaboury wrote:
>>> Dear group,
>>>
>>>
>>>
>>> Here is my function :
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> position<-function(x)
>>>
>>> {
>>>
>>>
>>>
>>>
>>> pose<-
>> read.csv2((paste(c("LSCPos",x,".csv"),collapse="")),dec=".",sep=",",as
>>> .is=T,h=T,skip=1)[,c(4,8,14,15)]
>>>
>>>     pose$CREATED.DATE<-as.Date(pose$CREATED.DATE,"%d/%m/%y")
>>>
>>>     futures<-pose[-grep("USD",pose[,1]),]
>>>
>>>
>>>     colnames(futures)<-
>> c("CONTRAT","CREATED.DATE","QUANTITY","SETTLEMENT")
>>>
>>>     lme<-pose[grep("USD",pose[,1]),]
>>>
>>>     lme$DESCRIPTION<- gsub(" *(\\d+/\\d+/\\d+) *$", "",
>> lme$DESCRIPTION)
>>>
>>>     library(plyr)
>>>
>>>     PositionFut<-ddply(futures, c("CONTRAT","SETTLEMENT"), summarise,
>>> POSITION=sum(QUANTITY))[,c(1,3,2)]
>>>
>>>     PositionLme<-unique(ddply(lme, c("DESCRIPTION"), summarise,
>>>
>> POSITION=sum(QUANITY),DATE=max(CREATED.DATE),SETTLEMENT=CLOSING.PRICE[C
>> REATE
>>> D.DATE == max(CREATED.DATE)]))[,-3]
>>>
>>>
>>>
>>>
>>>
>>>     print(PositionFut)
>>>
>>>     print(PositionLme)
>>>
>>>
>>>
>>> }
>>>
>>>
>>>
>>> As I'm totally new to this kind of sport, i.e. writing code, the code
>> is
>>> maybe ugly, but it does work. Now, one thing is missing.
>>>
>>>    I want to have the 2 Dataframes "PositionFut"&   "PositionLme" in my
>>> environment, as I will need them later for another function. If I do
>> a
>>> save(), it will be saved in my directory, but won't see it when I do
>> ls(),
>>> even if I do a get() before. What is the process to save these 2
>> objects in
>>> order to access them later in another function?
>>>
>>>
>>>
>>>
>>>
>>> ***************************
>>>
>>> Arnaud Gaboury
>>>
>>> Mobile: +41 79 392 79 56
>>>
>>> BBM: 255B488F
>>>
>>> ***************************
>>>
>>>
>>>
>>>
>>> 	[[alternative HTML version deleted]]
>>>
>>> ______________________________________________
>>> 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