[R] replace character by numeric value
Ivan Calandra
|v@n@c@|@ndr@ @end|ng |rom |e|z@@de
Thu Sep 28 08:17:15 CEST 2023
Dear Arnaud,
I don't quite unterstand why you have imbricated ifelse() statements. Do
you have more that BUY (1) and SELL (-1)? If not, why not simply:
mynewdf2 <- mydf2 |> dplyr::mutate(side = ifelse(side == 'BUY', 1, -1))
That would solve the problem. I'm not quite sure exactly what happens,
but this is probably related to the intermediary result after the first
ifelse(), where characters and numeric are mixed. But conversion to
numeric works properly, so I'm not sure what you meant:
as.numeric(mynewdf2$side)
More generally, why are you trying to convert to 1 and -1? Why not use
factors? Are you trying to test contrasts maybe? I would be surprised if
the function for the statistical test you are trying to use does not
deal with that already on its own.
HTH,
Ivan
On 27/09/2023 13:01, arnaud gaboury wrote:
> 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
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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