[R] data frame manipulation change elements meeting criteria

arnaud Gaboury arnaud.gaboury at gmail.com
Wed May 26 19:43:27 CEST 2010

Dear group,

Here is my df :

trade <-
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. =
-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")

Here is what I want :

If trade$Trade.Status=="DEL": then if trade$buy.Sell..Cleared==Sell , change
it to "Buy", if trade$buy.Sell..Cleared==Buy, change it to "Sell".
If trade$Trade.Status=="INS", do nothing
I tried to work around with ifelse, but don't know how to deal with so many

Any help is appreciated.


More information about the R-help mailing list