Home > Software engineering >  How can I create a new variable in Julia using an ifelse condition dataframe?
How can I create a new variable in Julia using an ifelse condition dataframe?

Time:10-27

I am learning Julia and I want to create a new variable (nota) using an if-else function with the transform! function from the DataFrames package.

Here is my code:

using DataFrames
datos = DataFrame(nombre =["ANGELICA","BRENDA","LILIANA","MARCO","FABIAN","MAURICIO"],
    grupo = ["A","A","B","B","C","C"],
    puntaje = [10,9,8,8,9,7]);
transform!(datos, :puntaje => ifelse(datos[datos.puntaje .< 9,:],"Suficiente","Excelente") => :nota)

but this error displays:

ERROR: TypeError: non-boolean (BitVector) used in boolean context

Stacktrace:

[1] top-level scope

@ REPL[23]:1

How can I solve it?

Thanks for your help

CodePudding user response:

Two problems:

  1. You are mixing ByRow with normal transform which is by column
  2. You can't mutate type of puntaje column

You probably want to do this:

julia> datos.puntaje = ifelse.(datos.puntaje .< 9, "Suficiente", "Excelente")
6-element Vector{String}:
 "Excelente"
 "Excelente"
 "Suficiente"
 "Suficiente"
 "Excelente"
 "Suficiente"

julia> datos
6×3 DataFrame
 Row │ nombre    grupo   puntaje    
     │ String    String  String     
─────┼──────────────────────────────
   1 │ ANGELICA  A       Excelente
   2 │ BRENDA    A       Excelente
   3 │ LILIANA   B       Suficiente
   4 │ MARCO     B       Suficiente
   5 │ FABIAN    C       Excelente
   6 │ MAURICIO  C       Suficiente
  • Related