Message-ID: <CAAxdm-587VZ+UpxwBaHh9ZTv_8gLN85bbWPgVm08tBB7699E-Q@mail.gmail.com>
Date: 2012-09-26T12:17:22Z
From: jim holtman
Subject: Change in order of names after applying "plyr" package
In-Reply-To: <1348649756.65859.YahooMailClassic@web124503.mail.ne1.yahoo.com>
Here is one of the places that you need a 'factor' to create the
ordering you want; notice the statement that I added:
> equity_data = data.frame(security_id = c("Air", "Air", "Air", "Air"
+ , "Air", "Air", "Air", "Air", "Air", "Air", "Air", "Air", "AB"
+ , "AB", "AB", "AB", "AB", "AB", "AB", "AB", "AB", "AB", "AB"
+ , "AB", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD"
+ , "AD", "AD", "AD")
+ , ason_date = c("10-Jan-12","9-Jan-12","8-Jan-12", "7-Jan-12"
+ , "6-Jan-12","5-Jan-12","4-Jan-12","3-Jan-12","2-Jan-12"
+ ,"1-Jan-12", "31-Dec-11", "30-Dec-11", "10-Jan-12","9-Jan-12"
+ ,"8-Jan-12", "7-Jan-12", "6-Jan-12","5-Jan-12","4-Jan-12"
+ ,"3-Jan-12","2-Jan-12","1-Jan-12", "31-Dec-11", "30-Dec-11"
+ , "10-Jan-12","9-Jan-12","8-Jan-12", "7-Jan-12", "6-Jan-12"
+ ,"5-Jan-12","4-Jan-12","3-Jan-12","2-Jan-12","1-Jan-12"
+ , "31-Dec-11", "30-Dec-11")
+ , security_rate = c(0.597,0.61,0.6,0.63,0.67,0.7,0.74,0.735
+ , 7.61,0.795,0.796, 0.84, 8.5,8.1,8.9,8.9,8.9,9,9,9,9,9,9,9
+ ,3.21,3.22,3.12, 3.51, 3.5, 3.37, 3.25, 3, 3.07, 3, 2.94, 2.6)
+ )
>
> # create a factor with the specified ordering
> equity_data$security_id <- factor(equity_data$security_id
+ , levels = c("Air", "AB", "AD")
+ )
Here are the results:
> result_method_other
$Air
[1] 6.229422
$AB
[1] 0.2355425
$AD
[1] 0.2918782
attr(,"split_type")
[1] "data.frame"
attr(,"split_labels")
security_id
1 Air
2 AB
3 AD
On Wed, Sep 26, 2012 at 4:55 AM, Vincy Pyne <vincy_pyne at yahoo.ca> wrote:
> Dear R helpers
>
> I have following two data.frames viz. equity_data and param.
>
> equity_data = data.frame(security_id = c("Air", "Air", "Air", "Air", "Air", "Air", "Air", "Air", "Air", "Air", "Air", "Air", "AB", "AB", "AB", "AB", "AB", "AB", "AB", "AB", "AB", "AB", "AB", "AB", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD", "AD"), ason_date = c("10-Jan-12","9-Jan-12","8-Jan-12", "7-Jan-12", "6-Jan-12","5-Jan-12","4-Jan-12","3-Jan-12","2-Jan-12","1-Jan-12", "31-Dec-11", "30-Dec-11", "10-Jan-12","9-Jan-12","8-Jan-12", "7-Jan-12", "6-Jan-12","5-Jan-12","4-Jan-12","3-Jan-12","2-Jan-12","1-Jan-12", "31-Dec-11", "30-Dec-11", "10-Jan-12","9-Jan-12","8-Jan-12", "7-Jan-12", "6-Jan-12","5-Jan-12","4-Jan-12","3-Jan-12","2-Jan-12","1-Jan-12", "31-Dec-11",
> "30-Dec-11"), security_rate = c(0.597,0.61,0.6,0.63,0.67,0.7,0.74,0.735, 7.61,0.795,0.796, 0.84, 8.5,8.1,8.9,8.9,8.9,9,9,9,9,9,9,9,3.21,3.22,3.12, 3.51, 3.5, 3.37, 3.25, 3, 3.07, 3, 2.94, 2.6))
>
> param = data.frame(confidence_level = c(0.99), holding_period = c(10), calculation_method = "MC", no_simulation_mc = c(100))
>
>
> library(plyr)
> library(reshape2)
>
> attach(equity_data)
> attach(param)
>
> security_names = unique(equity_data$security_id)
> # (security_names are used further in R code not included here)
>
> alpha = param$confidence_level
> t = param$holding_period
> n = param$no_simulation_mc
> method = param$calculation_method
>
>
> mc_VaR = function(security_id, ason_date, security_rate)
> {
> security_rate_returns <- NULL
> for (i
> in(1:length(ason_date)-1))
> {
> security_rate_returns[i] = log(security_rate[i]/security_rate[i+1])
> }
>
> return_mean = mean(security_rate_returns)
> return_sd = sd(security_rate_returns)
> simulation = rnorm(n, return_mean, return_sd)
> qq = sort(simulation, decreasing = TRUE)
> VaR_mc = -qq[alpha * n]*sqrt(t)
> return(VaR_mc)
> }
>
>
> result_method_other <- dlply(.data = equity_data, .variables = "security_id", .fun = function(x)
>
> mc_VaR(ason_date = x$ason_date, security_id = x$security_id,
> security_rate = x$security_rate))
>
>
>> result_method_other
> $AB
> [1] 0.2657424
>
> $AD
> [1] 0.212061
>
> $Air
> [1] 6.789733
>
> attr(,"split_type")
> [1] "data.frame"
> attr(,"split_labels")
> security_id
> 1 AB
> 2 AD
> 3 Air
>
>
> MY PROBLEM :
>
> My original data (i.e. equity_data) has the order of "Air", "AB" and "AD". However, after applying plyr, my order (and corresponding result) has changed to "AB", "AD" "Air".
>
>
> I need to
> maintain my original order of "Air", "AB" and "AD". How do I modify my R code for this?
>
> Kindly guide
>
>
> Vincy
>
>
> [[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.
>
--
Jim Holtman
Data Munger Guru
What is the problem that you are trying to solve?
Tell me what you want to do, not how you want to do it.