0

I want to create a function that cleans some messy dataframes, and I need to specify the names of the columns according to some inputs using ifelse.

  aporte_apv <- c("Fondo", "Serie", "Cantidad.de.Aportes")
  aporte_ffmm <- c("Rut.Cliente", "Cuenta", "Fecha")

colnames(base) <- ifelse(tolower(transaccion)=="apv", aporte_apv, 
                         ifelse(tolower(transaccion)=="ffmm", aporte_ffmm, stop("not valid inputs")))

Apparently ifelse can't handle vectors, how can I do this in an elegant way?

  • 2
    *"Apparently ifelse can't handle vectors"* Nonsense. What makes you believe that's the case? `ifelse` is a vectorised function. – Maurits Evers Apr 06 '18 at 14:42
  • We will need to know what `base` and `transaccion` look like in order to help you - can you please include workable samples of these objects in an edit? – 93i7hdjb Apr 06 '18 at 14:45
  • Maybe i'm wrong, but how can I replace the columns names with it? it only takes the first element. Do you know what I'm doing worng? – Gabriel Gajardo Apr 06 '18 at 14:45
  • `ifelse` returns a value with the same shape as your (logical) test object. In other words, if your test object has length 1, so has the return object. The answer lies in the structure of `base` and `transaccion`. – Maurits Evers Apr 06 '18 at 14:47
  • thanks! that's what i needed :) – Gabriel Gajardo Apr 06 '18 at 14:56
  • Note that you can assign the result inside the `ifelse` and get a vector (see my answer on the post linked) – Cath Apr 06 '18 at 15:07

0 Answers0