arnaud Gaboury
2010-May-26 17:43 UTC
[R] data frame manipulation change elements meeting criteria
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. 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")
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
conditions.
Any help is appreciated.
TY
Joris Meys
2010-May-26 19:14 UTC
[R] data frame manipulation change elements meeting criteria
see ?switch
X<- rep(c("Buy","Sell","something
else"),each=5)
Y<- rep(c("DEL","INS","DEL"),5)
new.vect <- X
for (i in which(Y=="DEL")){
new.vect[i]<-switch(
EXPR = X[i],
Sell="Buy",
Buy="Sell",
X[i])
}
cbind(new.vect,X,Y)
On Wed, May 26, 2010 at 7:43 PM, arnaud Gaboury
<arnaud.gaboury@gmail.com>wrote:
> 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. > 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")
>
> 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
> conditions.
>
> Any help is appreciated.
>
> TY
>
> ______________________________________________
> 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.
>
--
Joris Meys
Statistical Consultant
Ghent University
Faculty of Bioscience Engineering
Department of Applied mathematics, biometrics and process control
Coupure Links 653
B-9000 Gent
tel : +32 9 264 59 87
Joris.Meys@Ugent.be
-------------------------------
Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php
[[alternative HTML version deleted]]