[R] switch function
arnaud Gaboury
arnaud.gaboury at gmail.com
Thu May 27 14:37:33 CEST 2010
Thanks to Joris, this line does what I want :
trades$Buy.Sell..Cleared.[which(trades$Trade.Status=="DEL")] <-
sapply(trades$Buy.Sell..Cleared.[which(trades$Trade.Status=="DEL")],
switch,Sell="Buy",Buy="Sell")
> -----Original Message-----
> From: jim holtman [mailto:jholtman at gmail.com]
> Sent: Thursday, May 27, 2010 2:34 PM
> To: arnaud Gaboury
> Cc: r-help at r-project.org
> Subject: Re: [R] switch function
>
> try this:
>
> > toBuy <- trades$Trade.Status == "DEL" & trades$Buy.Sell..Cleared. ==
> "Sell"
> > toSell <- trades$Trade.Status == "DEL" & trades$Buy.Sell..Cleared. ==
> "Buy"
> > x <- trades # make a copy to change
> > x$Buy.Sell..Cleared.[toBuy] <- "Buy"
> > x$Buy.Sell..Cleared.[toSell] <- "Sell"
> > x
> Trade.Status Instrument.Long.Name Delivery.Prompt.Date
> Buy.Sell..Cleared. Volume Price Net.Charges..sum.
> 1 DEL SUGAR NO.11 Jul/10
> Buy 1 15.2500 4.01
> 2 INS CORN Jul/10
> Buy 2 368.0000 -8.64
> 3 INS CORN Jul/10
> Buy 1 368.5000 -4.32
> > trades
> Trade.Status Instrument.Long.Name Delivery.Prompt.Date
> Buy.Sell..Cleared. Volume Price Net.Charges..sum.
> 1 DEL SUGAR NO.11 Jul/10
> Sell 1 15.2500 4.01
> 2 INS CORN Jul/10
> Buy 2 368.0000 -8.64
> 3 INS CORN Jul/10
> Buy 1 368.5000 -4.32
> >
> >
>
>
> On Thu, May 27, 2010 at 4:03 AM, arnaud Gaboury
> <arnaud.gaboury at gmail.com> wrote:
> > Dear group,
> >
> > Here is my df :
> >
> > trades <-
> > structure(list(Trade.Status = c("DEL", "INS", "INS"),
> Instrument.Long.Name =
> > c("SUGAR NO.11",
> > "CORN", "CORN"), Delivery.Prompt.Date = c("Jul/10", "Jul/10",
> > "Jul/10"), Buy.Sell..Cleared. = c("Sell", "Buy", "Buy"), Volume =
> c(1L,
> > 2L, 1L), Price = c("15.2500", "368.0000", "368.5000"),
> Net.Charges..sum. =
> > c(4.01,
> > -8.64, -4.32)), .Names = c("Trade.Status", "Instrument.Long.Name",
> > "Delivery.Prompt.Date", "Buy.Sell..Cleared.", "Volume", "Price",
> > "Net.Charges..sum."), row.names = c(NA, 3L), class = "data.frame")
> >
> > I want to replace "Buy" by "Sell" and "Sell" by "Buy" in column
> > "Buy.Sell..Cleared." when element in column "Trade.Status" is equal
> to
> > "DEL".
> >
> > I think I can write something like this :
> >
> >
> >>tradesnew<-
> sapply(trades$Buy.Sell..Cleared[which(trades$Buy.Sell..Cleared==
> > "DEL"),],switch,.......) but I don't really know how to pass further
> > arguments to the switch function.
> >
> > Any help is appreciated.
> >
> > ______________________________________________
> > 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
> Cincinnati, OH
> +1 513 646 9390
>
> What is the problem that you are trying to solve?
More information about the R-help
mailing list