Home > Blockchain >  Apply custom functions to a column depending on values from other columns
Apply custom functions to a column depending on values from other columns

Time:12-19

I am trying to apply custom functions to a column in my data frame. Basically what I am trying to do is apply a function if the value of SPP column is one (say anchoveta), and apply a different function is the value of SPP is (i.e merluza), and create a new column called TALLA.

My data frame looks like this:

` Label            Year Month Season SPP_depre       SPP    PROMEDIO DESVIACIÓN
1 OB-03-2015-002-1 2015     3      1        OB  pejerrey    3.04       0.04
2 OB-03-2015-002-2 2015     3      1        OB anchoveta    2.48       0.03
3 OB-03-2015-003   2015     3      1        OB   merluza    3.40       0.03
4 OB-03-2015-008-1 2015     3      1        OB anchoveta    2.39       0.02
5 OB-03-2015-008-2 2015     3      1        OB anchoveta    2.75       0.03
6 OB-03-2015-008-3 2015     3      1        OB anchoveta    2.43       0.02`

I have defined my functions like this: `

ancho <- function(PROMEDIO){
  TALLA=3.33*PROMEDIO 0.798
  return(TALLA)
}

#Pejerrey
peje <- function(PROMEDIO){
  TALLA=3.85*PROMEDIO-0.614
  return(TALLA)
}

#Merluza
mer <-function(PROMEDIO){
  TALLA=28.82*PROMEDIO-37.331
  return(TALLA)
}

I don't know if a loop is the best way to go. Would very much appreciate the help.

I want the output to look like this:

` Label            Year Month Season SPP_depre       SPP    PROMEDIO DESVIACIÓN TALLA
1 OB-03-2015-002-1 2015     3      1        OB  pejerrey    3.04       0.04     11.09
2 OB-03-2015-002-2 2015     3      1        OB anchoveta    2.48       0.03     9.056
3 OB-03-2015-003   2015     3      1        OB   merluza    3.40       0.03     60.65
4 OB-03-2015-008-1 2015     3      1        OB anchoveta    2.39       0.02
5 OB-03-2015-008-2 2015     3      1        OB anchoveta    2.75       0.03
6 OB-03-2015-008-3 2015     3      1        OB anchoveta    2.43       0.02`

I have been trying to create a loop function but I get lost.

CodePudding user response:

Libraries

library(dplyr)

Data

data <-
  read.table(
    header = TRUE,
    text = "Label            Year Month Season SPP_depre       SPP    PROMEDIO DESVIACIÓN
1 OB-03-2015-002-1 2015     3      1        OB  pejerrey    3.04       0.04
2 OB-03-2015-002-2 2015     3      1        OB anchoveta    2.48       0.03
3 OB-03-2015-003   2015     3      1        OB   merluza    3.40       0.03
4 OB-03-2015-008-1 2015     3      1        OB anchoveta    2.39       0.02
5 OB-03-2015-008-2 2015     3      1        OB anchoveta    2.75       0.03
6 OB-03-2015-008-3 2015     3      1        OB anchoveta    2.43       0.02"
  )

Code

data %>% 
  mutate(TALLA = case_when(
    SPP == "anchoveta" ~ ancho(PROMEDIO),
    SPP == "pejerrey" ~ peje(PROMEDIO),
    SPP == "merluza" ~ mer(PROMEDIO)
  )
 )

CodePudding user response:

If you just call the functions the names of the groups, you can use base R and get:

data <-
  read.table(
    header = TRUE,
    text = "Label            Year Month Season SPP_depre       SPP    PROMEDIO DESVIACIÓN
1 OB-03-2015-002-1 2015     3      1        OB  pejerrey    3.04       0.04
2 OB-03-2015-002-2 2015     3      1        OB anchoveta    2.48       0.03
3 OB-03-2015-003   2015     3      1        OB   merluza    3.40       0.03
4 OB-03-2015-008-1 2015     3      1        OB anchoveta    2.39       0.02
5 OB-03-2015-008-2 2015     3      1        OB anchoveta    2.75       0.03
6 OB-03-2015-008-3 2015     3      1        OB anchoveta    2.43       0.02"
  )

anchoveta <- function(PROMEDIO){
  TALLA=3.33*PROMEDIO 0.798
  return(TALLA)
}


pejerrey <- function(PROMEDIO){
  TALLA=3.85*PROMEDIO-0.614
  return(TALLA)
}

merluza <-function(PROMEDIO){
  TALLA=28.82*PROMEDIO-37.331
  return(TALLA)
}


data$TALLA <- unlist(Map(\(x,y) get(x)(y), data$SPP, data$PROMEDIO))

data
#>              Label Year Month Season SPP_depre       SPP PROMEDIO DESVIACIÓN
#> 1 OB-03-2015-002-1 2015     3      1        OB  pejerrey     3.04       0.04
#> 2 OB-03-2015-002-2 2015     3      1        OB anchoveta     2.48       0.03
#> 3   OB-03-2015-003 2015     3      1        OB   merluza     3.40       0.03
#> 4 OB-03-2015-008-1 2015     3      1        OB anchoveta     2.39       0.02
#> 5 OB-03-2015-008-2 2015     3      1        OB anchoveta     2.75       0.03
#> 6 OB-03-2015-008-3 2015     3      1        OB anchoveta     2.43       0.02
#>     TALLA
#> 1 11.0900
#> 2  9.0564
#> 3 60.6570
#> 4  8.7567
#> 5  9.9555
#> 6  8.8899
  • Related