Skip to content

CRAN R Help

1 message · arun

#
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.