0

I want to change all the zeros (0) and 36.1 in a given variable to another number.

To give you a better picture, this is what i tried to do:

transacciones_diarias$TIPO_CAMBIO_POOL <- ifelse(transacciones_diarias$TIPO_CAMBIO_POOL %in% c(0,36.1) & transacciones_diarias$COD_TIPOMOV == "VENTA",
                                                 transacciones_diarias$MONTO_TIPOCAMBIOCOL-0.01,
                                                    ifelse(transacciones_diarias$TIPO_CAMBIO_POOL %in% c(0,36.1) & transacciones_diarias$COD_TIPOMOV == "COMPRA",
                                                           transacciones_diarias$MONTO_TIPOCAMBIOCOL+0.01,
                                                           transacciones_diarias$TIPO_CAMBIO_POOL),
                                                 transacciones_diarias$TIPO_CAMBIO_POOL)

The error is:

Error in ifelse(transacciones_diarias$TIPO_CAMBIO_POOL %in% c(0, 36.1) &  : 
  unused argument (transacciones_diarias$TIPO_CAMBIO_POOL)

Here is some sample data:

enter image description here

Expected result:

enter image description here

Ivancito
  • 159
  • 1
  • 11
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Oct 29 '19 at 20:31
  • Pictures of data are not helpful because we cannot copy/paste the data for testing. See the link I provided for how to share data in a reproducible format. If you got errors with your method, be sure to include those. Also it seems like you are using variables like `TIPO_CAMBIO_POOL ` without referring to the data.frame name `transacciones_diarias$` which generally doesn't work unless you are using `transform()` or `dplyr::mutate()` – MrFlick Oct 29 '19 at 20:43
  • Im trying to get some sample data, but i cant. I'll keep trying. – Ivancito Oct 29 '19 at 21:05

1 Answers1

1

I think you can accomplish what you want with dplyr::case_when. This should work as desired, but I can't test without data. Hope it helps.

new_df <- df %>%
  mutate(TIPO_CAMBIO_POOL = case_when(
    !TIPO_CAMBIO_POOL %in% c(0, 36.1) ~ TIPO_CAMBIO_POOL, # if values aren't 0 or 36.1, keep them.
    TIPO_CAMBIO_POOL %in% c(0, 36.1) & COD_TIPOMOV == 'COMPRA' ~ MONTO_TIOPCAMBIOCOL -0.01, 
    TIPO_CAMBIO_POOL %in% c(0, 36.1) & COD_TIPOMOV == 'VENTA' ~ MONTO_TIOPCAMBIOCOL +0.01
    ))

EDIT: This will add/subtract 0.01 from the MONTO column based on the COD_TIPOMOV value.

TTS
  • 1,818
  • 7
  • 16
  • I dont want an specific number, I need it to be: TIPO_CAMBIO_POOL + 0.01 or TIPO_CAMBIO_POOL - 0.01. I tried to edit the the code and it just returned +0.01 or -0.01 once I modified it. – Ivancito Oct 30 '19 at 13:17
  • 1
    TIPO_CAMBIO_POOL = 0 in those places, so that makes sense. – TTS Oct 30 '19 at 13:49
  • 1
    Oh, that's true. Im supposed to sum it to MONTO_TIPOCAMBIOCOL. Thanks! – Ivancito Oct 30 '19 at 13:52