[R] CRAN R Help

arun smartpink111 at yahoo.com
Thu Mar 28 03:42:38 CET 2013


Dear Katherine,

For your first question:

If you are creating these files in a specific folder, then
list.files()
#[1] "Individual_Present_Value_BONDS.csv"  "Individual_Present_Value_Equity.csv"
#[3] "Individual_Present_Value_FOREX.csv" 

gives you which files are present.  But, suppose you have other files too in the folder and you want to check only the above mentioned files.

I added one more file in the working folder.
 list.files()
#[1] "Individual_Present_Value_BONDS.csv"  "Individual_Present_Value_Equity.csv"
#[3] "Individual_Present_Value_FOREX.csv"  "test.txt"  

list.files()[grep("BONDS|Equity|FOREX", list.files())] #only the concerned files could be seen
#[1] "Individual_Present_Value_BONDS.csv"  "Individual_Present_Value_Equity.csv"
#[3] "Individual_Present_Value_FOREX.csv" 

#Removed ..._BONDS.csv" from the folder.
list.files()[grep("BONDS|Equity|FOREX", list.files())]
#[1] "Individual_Present_Value_Equity.csv" "Individual_Present_Value_FOREX.csv" 

Second question: (added the ..._BONDS.csv into the folder)

#Read the 3 files in a list
lst1<- lapply(list.files()[grep("BONDS|Equity|FOREX", list.files())],function(x) {x1<-read.csv(x,sep=",",stringsAsFactors=FALSE); colnames(x1)[1]<- "simulations";x1}) # one of the dataset had "Simulations" as first column

#Merge with a subset of data 

library(plyr)
lstSub<- lapply(list.files()[grep("BONDS|Equity|FOREX",list.files())],function(x) {x1<-head(read.csv(x,sep=",",stringsAsFactors=FALSE)); colnames(x1)[1]<- "simulations";x1}) #subset


 join_all(lstSub,type="inner",by="simulations") # depending on your needs, you can change to type="full"

head(join_all(lstSub,type="inner",by="simulations"),2) #looks like columns are in the same order as in the individual dataset
#      simulations T_Bond_14.50_2011A T_Bond_14.50_2012B T_Bond_15.50_2010D
#1      Current_PV           184300.7           178906.3           104177.5
#2 Simualtion_no_1           184545.6           178712.6           104431.7
 # T_Bond_15.50_2010E T_Bond_15.50_2011B Equity_A Equity_B Equity_C Equity_D
#1           225164.0            1006044 113718.3 392252.5 344494.2  6074407
#2           225727.8            1006456 114838.4 397470.2 346675.5  5943718
 # Trans_no.1.Sell.USD Trans_no.2.Sell.USD Trans_no.3.Buy.AUD
#1            -5819794           -15122103           78567139
#2            -6937125           -17368874           78217006
 # Trans_no.4.Sell.USD Trans_no.5.Buy.EURO Trans_no.6.Sell.USD
#1           -18621066            10340611           -24023002
#2           -21423091            16831727           -27356539
 # Trans_no.7.Buy.EURO Trans_no.8.Sell.USD Trans_no.9.Buy.GBP
#1            10340611           -24023002          -20486447
#2            16831727           -27356539          -18408896
 # Trans_no.10.Sell.USD Trans_no.11.Buy.CHF Trans_no.12.Sell.USD
#1            -48273317             5435002             -4647643
#2            -54892776             5652627             -5279506
 # Trans_no.13.Sell.USD
#1             -8657418
#2             -9780783

I hope this helps.
A.K.



________________________________
 From: Katherine Gobin <katherine_gobin at yahoo.com>
To: smartpink111 at yahoo.com 
Sent: Wednesday, March 27, 2013 6:17 PM
Subject: CRAN R Help
 

Dear Mr Arun,
 
Sorry to bother you again. Last week you had given me the
question about what will happen to my R – code if say bonds or equity or forex something
is missing?.
Your  this question
has acted like CLUE to me and accordingly I had developed the R –code. And I
think now it’s working  (at least as of
now as R has always thrown some surprises ahead of me and hence I am using the phrase
“at least as of now”). 

However, although I had managed say about 95% of the job, I
need one small favor from you if possible.

I have three output csv files and the names are as given
below.
 
individual_Present_Value_BONDS.csv
individual_Present_Value_Equity.csv
individual_Present_Value_FOREX.csv

I have attached these files in zip format for your
reference. Each of these files have first column as common column. So I can
combine these files (after reading these as data.frames) using merge command. 

However, there are two problems. 



(1) I am not sure if the file exists or not.
Depending on the presence in portfolio, file is created. E.g. if there is no
Equity, ‘individual_Present_Value_Equity.csv’ will not be created. So how do I
merge these files only after testing if the file exists or not.


(2)    Also, assuming the files are merged, then the
columns in the merged data.frame are not in the same order as they are in the
individual files. i.e. the columns get mixed up in the 


For me the first task is more
important. Will it be possible for you to help me out.  Sorry again for writing directly to you. I
wrote directly to you because you are already aware of my problem and secondly,
I don’t know if we can attach the files to the R forum mails.
Sorry to bother you once again.
 
Regards
 
Katherine



More information about the R-help mailing list