[R] replace character by numeric value

Ebert,Timothy Aaron tebert @end|ng |rom u||@edu
Fri Sep 29 16:12:35 CEST 2023


You might also try
mynewdf <- mydf |> dplyr::mutate(side = ifelse(side == 'BUY', 1,-1))

This may give unexpected results if there are other choices besides "BUY" and "SELL".

mynewdf <- mydf |> dplyr::mutate(side = ifelse(side == 'BUY', 1, ifelse(side == 'SELL', -1, NA)))

is an option that would take care of other options.



-----Original Message-----
From: R-help <r-help-bounces using r-project.org> On Behalf Of Ben Bolker
Sent: Friday, September 29, 2023 10:00 AM
To: r-help using r-project.org
Subject: Re: [R] replace character by numeric value

[External Email]

   The reason you're getting the result as character is that you have 'side' as your alternative result in the second ifelse().  If "BUY" and "SELL" are the only options you might try

    ifelse(side == 'BUY', 1, ifelse(side == 'SELL', -1, NA))

or

    c(1,-1)[match(side, c("BUY", "SELL"))]

or

vals <- c(BUY=1, SELL = -1)
vals[side]


On 2023-09-29 9:21 a.m., Ebert,Timothy Aaron wrote:
> Does this work?
> mynewdf$side <- as.numeric(mynewdf$side)
>
> This code would be the next line after your mutate.
>
> TIm
>
> -----Original Message-----
> From: R-help <r-help-bounces using r-project.org> On Behalf Of Enrico
> Schumann
> Sent: Thursday, September 28, 2023 3:13 AM
> To: arnaud gaboury <arnaud.gaboury using gmail.com>
> Cc: r-help <r-help using r-project.org>
> Subject: Re: [R] replace character by numeric value
>
> [External Email]
>
> On Wed, 27 Sep 2023, arnaud gaboury writes:
>
>> I have two data.frames:
>>
>> mydf1 <- structure(list(symbol = "ETHUSDT", cummulative_quote_qty =
>> 1999.9122, side = "BUY", time = structure(1695656875.805, tzone = "",
>> class = c("POSIXct", "POSIXt"))), row.names = c(NA, -1L), class =
>> c("data.table",
>> "data.frame"))
>>
>> mydf2 <- structure(list(symbol = c("ETHUSDT", "ETHUSDT", "ETHUSDT"),
>> cummulative_quote_qty = c(1999.119408, 0, 2999.890985), side =
>> c("SELL", "BUY", "BUY"), time = structure(c(1695712848.487,
>> 1695744226.993, 1695744509.082), class = c("POSIXct", "POSIXt"
>> ), tzone = "")), row.names = c(NA, -3L), class = c("data.table",
>> "data.frame"))
>>
>> I use this line to replace 'BUY' by numeric 1 and 'SELL' by numeric
>> -1 in
>> mydf1 and mydf2:
>> mynewdf <- mydf |> dplyr::mutate(side = ifelse(side == 'BUY', 1,
>> ifelse(side == 'SELL', -1, side)))
>>
>> This does the job but I am left with an issue: 1 and -1 are
>> characters for
>> mynewdf2 when it is numeric for mynewdf1. The result I am expecting
>> is getting numeric values.
>> I can't solve this issue (using as.numeric(1) doesn't work) and don't
>> understand why I am left with num for mynewdf1 and characters for mynewdf2.
>>
>>> mynewdf1 <- mydf1 |> dplyr::mutate(side = ifelse(side == 'BUY', 1,
>> ifelse(side == 'SELL', -1, side)))
>>> str(mynewdf1)
>> Classes 'data.table' and 'data.frame': 1 obs. of  4 variables:
>>   $ symbol               : chr "ETHUSDT"
>>   $ cummulative_quote_qty: num 2000
>>   $ side                 : num 1      <<<------
>>   $ time                 : POSIXct, format: "2023-09-25 17:47:55"
>>   - attr(*, ".internal.selfref")=<externalptr>
>>
>>> mynewdf2 <- mydf2 |> dplyr::mutate(side = ifelse(side == 'BUY', 1,
>> ifelse(side == 'SELL', -1, side)))
>>>   str(mynewdf2)
>> Classes 'data.table' and 'data.frame': 3 obs. of  4 variables:
>>   $ symbol               : chr  "ETHUSDT" "ETHUSDT" "ETHUSDT"
>>   $ cummulative_quote_qty: num  1999 0 3000
>>   $ side                 : chr  "-1" "1" "1"   <<<------
>>   $ time                 : POSIXct, format: "2023-09-26 09:20:48"
>> "2023-09-26 18:03:46" "2023-09-26 18:08:29"
>>   - attr(*, ".internal.selfref")=<externalptr>
>>
>> Thank you for help
>>
>
> I'd use something like this:
>
>      map <- c(BUY = 1, SELL = -1)
>      mydf1$side <- map[mydf1$side]
>      str(mydf1)
>      ## Classes 'data.table' and 'data.frame':   1 obs. of  4 variables:
>      ##  $ symbol               : chr "ETHUSDT"
>      ##  $ cummulative_quote_qty: num 2000
>      ##  $ side                 : num 1
>
>      mydf2$side <- map[mydf2$side]
>      str(mydf2)
>      ## Classes 'data.table' and 'data.frame':   3 obs. of  4 variables:
>      ##  $ symbol               : chr  "ETHUSDT" "ETHUSDT" "ETHUSDT"
>      ##  $ cummulative_quote_qty: num  1999 0 3000
>      ##  $ side                 : num  -1 1 1
>      ##  $ time                 : POSIXct, format: "2023-09-26 09:20:48" ...
>
>
>
> --
> Enrico Schumann
> Lucerne, Switzerland
> http://enric/
> oschumann.net%2F&data=05%7C01%7Ctebert%40ufl.edu%7Cc444bc296379414ba95
> b08dbc0f46826%7C0d4da0f84a314d76ace60a62331e1b84%7C0%7C0%7C63831592818
> 5843893%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLC
> JBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=YHs9vJMita%2BWAdlmn
> zx7%2Bte8gGqf3bTDl6CjnVG0exw%3D&reserved=0
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat/
> .ethz.ch%2Fmailman%2Flistinfo%2Fr-help&data=05%7C01%7Ctebert%40ufl.edu
> %7Cc444bc296379414ba95b08dbc0f46826%7C0d4da0f84a314d76ace60a62331e1b84
> %7C0%7C0%7C638315928185843893%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAw
> MDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sda
> ta=ANxrud%2Bvq7xIraYkKFA4FLgJT3%2Bp1ceynsEmvnl7ot0%3D&reserved=0
> PLEASE do read the posting guide
> http://www.r/
> -project.org%2Fposting-guide.html&data=05%7C01%7Ctebert%40ufl.edu%7Cc4
> 44bc296379414ba95b08dbc0f46826%7C0d4da0f84a314d76ace60a62331e1b84%7C0%
> 7C0%7C638315928185843893%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiL
> CJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=CW
> oJHfmd4Nrll13MSqD22gRVORw8sOxKbvz1rFI8ov0%3D&reserved=0
> and provide commented, minimal, self-contained, reproducible code.
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat/
> .ethz.ch%2Fmailman%2Flistinfo%2Fr-help&data=05%7C01%7Ctebert%40ufl.edu
> %7Cc444bc296379414ba95b08dbc0f46826%7C0d4da0f84a314d76ace60a62331e1b84
> %7C0%7C0%7C638315928185843893%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAw
> MDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sda
> ta=ANxrud%2Bvq7xIraYkKFA4FLgJT3%2Bp1ceynsEmvnl7ot0%3D&reserved=0
> PLEASE do read the posting guide
> http://www.r/
> -project.org%2Fposting-guide.html&data=05%7C01%7Ctebert%40ufl.edu%7Cc4
> 44bc296379414ba95b08dbc0f46826%7C0d4da0f84a314d76ace60a62331e1b84%7C0%
> 7C0%7C638315928185843893%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiL
> CJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=CW
> oJHfmd4Nrll13MSqD22gRVORw8sOxKbvz1rFI8ov0%3D&reserved=0
> and provide commented, minimal, self-contained, reproducible code.

--
Dr. Benjamin Bolker
Professor, Mathematics & Statistics and Biology, McMaster University Director, School of Computational Science and Engineering
(Acting) Graduate chair, Mathematics & Statistics  > E-mail is sent at my convenience; I don't expect replies outside of working hours.

______________________________________________
R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.



More information about the R-help mailing list