Off course. You put in a matrix to sapply, but sapply is for vectors. You
want to apply the switch command on every entry of the vector
trades$Buy.Sell..Cleared for which trades$Trade.Status equals "DEL". Why do
you try to put in a matrix with all variables for the observations where
status is DEL?

You should have done :

tradesnew<-sapply(trades$Buy.Sell..Cleared[which(trades$Trade.Status=="DEL")],
     switch,Sell="Buy",Buy="Sell")

Check the help files, and keep track of what goes in and out a function.

Cheers
Joris

On Thu, May 27, 2010 at 9:41 AM, arnaud Gaboury <arnaud.gabo...@gmail.com>wrote:

> Joris,
>
> If i pass this line :
>
> >tradesnew<-sapply(trades[which(trades$Trade.Status=="DEL"),],switch,Sel
> >l="Buy",Buy="Sell")
>
> Here is what I get :
>
> > tradesnew
> $Trade.Status
> NULL
>
> $Instrument.Long.Name
> NULL
>
> $Delivery.Prompt.Date
> NULL
>
> $Buy.Sell..Cleared.
> [1] "Buy"
>
> $Volume
> [1] "Buy"
>
> $Price
> NULL
>
> $Net.Charges..sum.
> NULL
>
> That's certainly not what I want.
>
>
>
>
> From: Joris Meys [mailto:jorism...@gmail.com]
> Sent: Thursday, May 27, 2010 8:43 AM
> To: arnaud Gaboury
> Cc: r-help@r-project.org
> Subject: Re: [R] data frame manipulation change elements meeting criteria
>
> The loop is due to the switch statement, not the condition. Without
> condition it would become:
>
> for (i in 1:length(Y)){
>     new.vect[i]<-switch(
>           EXPR = X[i],
>           Sell="Buy",
>           Buy="Sell",
>           X[i])
> }
> You can make an sapply construct too off course :
>
> new.vect <- sapply(X[which(Y=="DEL")],switch,Sell="Buy",Buy="Sell")
>
> This will speed up things a little bit, but the effect is marginal.
> Cheers
> Joris
> On Thu, May 27, 2010 at 8:33 AM, arnaud Gaboury <arnaud.gabo...@gmail.com>
> wrote:
> Thank you for the answer.
> Is there any way to combine if() and switch() in one line? In my case,
> something like :
>
> >if(trade$Trade.Status=="DEL")switch(.....)
>
> I would like to avoid the loop .
>
>
>
> From: Joris Meys [mailto:jorism...@gmail.com]
> Sent: Wednesday, May 26, 2010 9:15 PM
> To: arnaud Gaboury
> Cc: r-help@r-project.org
> Subject: Re: [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.gabo...@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.m...@ugent.be
> -------------------------------
> Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php
>
>
>
> --
> 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.m...@ugent.be
> -------------------------------
> Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php
>
>


-- 
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.m...@ugent.be
-------------------------------
Disclaimer : http://helpdesk.ugent.be/e-maildisclaimer.php

        [[alternative HTML version deleted]]

______________________________________________
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.

Reply via email to