[R] switch function

arnaud Gaboury arnaud.gaboury at gmail.com
Thu May 27 10:03:59 CEST 2010


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.



More information about the R-help mailing list