Dear R forum helpers, I am extremely sorry for the receipt of my incomplete mail yesterday. There was connectivity problem at my end and so I chose to send the mail through my cell, only to realize today about the way mail has been transmitted. I am again sending my complete mail through regular channel and sincerely apologize for the inconvenience caused. ## Here is my actual mail Dear R forum helpers, I have following data trans <- data.frame(currency = c("EURO", "USD", "USD", "GBP", "USD", "AUD"), position_amt = c(10000, 25000, 20000, 15000, 22000, 30000)) date <- c("12/31/2010", "12/30/2010", "12/29/2010", "12/28/2010", "12/27/2010", "12/24/2010", "12/23/2010", "12/22/2010", "12/21/2010", "12/20/2010") USD <- c(112.05, 112.9, 110.85, 109.63, 108.08, 111.23, 112.49, 108.87, 109.33, 111.88) GBP <- c(171.52, 168.27,169.03, 169.64, 169.29, 169.47, 170.9, 168.69, 170.9, 169.96) EURO <- c(42.71, 42.68, 41.86, 44.71, 44.14, 44.58, 41.07, 42.23, 44.55, 41.12) CHF <- c(41.5, 41.47, 42.84, 43.44, 43.69, 42.3, 42.05, 41.23, 42.76, 43.79) AUD <- c(109.55, 102.52, 114.91, 122.48, 122.12, 123.96, 100.36, 110.19, 121.58, 103.46) These are the exchange rates and I am trying calculating the returns. I am giving only a small portion of actual data as I can't send this as an attachment. I am using function as I want to generalize the code for any portfolio. # __________________________________________________ # My Code trans <- read.table('transactions.csv', header=TRUE, sep=",", na.strings="NA", dec=".", strip.white=TRUE) # reading as table. #currency <- read.table('currency.csv') #date <- currency$date #USD = currency$USD #GBP = currency$GBP #EURO = currency$EURO #CHF = currency$CHF #AUD = currency$AUD # _________________________________________________________ # CREATION of Function. I am using function as no of transactions is not constant. spot_trans = function(currency_trans) { if (currency_trans == "USD") {rate = USD} # So if I am dealing with TRANSACTION "USD", I am selecting the USD exchange rates. if (currency_trans == "GBP") {rate = GBP} if (currency_trans == "EURO") {rate = EURO} if (currency_trans == "CHF") {rate = CHF} if (currency_trans == "AUD") {rate = AUD} # ________________________________________________ # CURRENCY Rate RETURNS i.e. lob(todays rate / yesterday rate) and the data is in descending "Date" order currency_rate_returns = NULL for (i in 1:(length(rate)-1)) # if there are 10 elements, total no of returns = 9 { currency_rate_returns[i] = log(rate[i]/rate[i+1]) } currency_rate_returns return(data.frame(returns = currency_rate_returns)) } # _______________________________________________ spot_returns_table <- lapply(1:nrow(trans), function(z) with(trans[z, ], spot_trans(currency_trans=trans$currency_transacted))) spot_returns_table This generates the output as given below with 30 warnings. Also, as there are six transactions, 6 outputs are generated but the output in all pertains only to the first transacations i.e. 6 times returns are generated for the first transaction "EURO" > warnings() Warning messages: 1: In if (currency_trans == "USD") { ... : the condition has length > 1 and only the first element will be used 2: In if (currency_trans == "GBP") { ... : the condition has length > 1 and only the first element will be used 3: In if (currency_trans == "EURO") { ... : the condition has length > 1 and only the first element will be used .... and so on The output is as given below. > spot_returns_table [[1]] spot_returns 1 0.0007026584 2 0.0193997094 3 -0.0658664732 4 0.0128307894 5 -0.0099189271 6 0.0820074000 7 -0.0278529410 8 -0.0534812850 9 0.0801175328 10 -0.0710983059 [[2]] spot_returns 1 0.0007026584 2 0.0193997094 3 -0.0658664732 4 0.0128307894 5 -0.0099189271 6 0.0820074000 7 -0.0278529410 8 -0.0534812850 9 0.0801175328 10 -0.0710983059 [[3]] spot_returns 1 0.0007026584 2 0.0193997094 3 -0.0658664732 4 0.0128307894 5 -0.0099189271 6 0.0820074000 .................... .................... and so on. Kindly guide as if there is only one transaction i.e. I am dealing with only one currency, code runs excellently. Thanking in advance and once again apologize for the inconvenience caused. Amelia Vettori [[alternative HTML version deleted]]
______________________________________________ R-help@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.