Transform operation using two or more columns in a DataFrame

Use either of the two below:

julia> transform(
           df,
           AsTable([:female_pop, :female_deaths]) => ByRow(
               x -> x.female_deaths / (x.female_pop + 0.5 * x.female_deaths)
           ) => :cond_prob2
       )
12×5 DataFrame
 Row │ age    female_deaths  female_pop  cond_prob    cond_prob2
     │ Int64  Int64          Float64     Float64      Float64
─────┼────────────────────────────────────────────────────────────
   1 │     0            136     48283.0  0.00281276   0.00281276
   2 │     1              8     47195.0  0.000169495  0.000169495
   3 │     2              3     48140.0  6.23163e-5   6.23163e-5
   4 │     3              5     50017.0  9.9961e-5    9.9961e-5
   5 │     4              2     49189.0  4.06587e-5   4.06587e-5
   6 │     5              6     49543.0  0.0001211    0.0001211
   7 │     6              6     49951.0  0.000120111  0.000120111
   8 │     7              2     49758.0  4.01937e-5   4.01937e-5
   9 │     8              2     49728.0  4.0218e-5    4.0218e-5
  10 │     9              3     49909.0  6.01076e-5   6.01076e-5
  11 │    10              2     49051.0  4.07731e-5   4.07731e-5
  12 │    11              1     49233.0  2.03114e-5   2.03114e-5

julia> transform(
           df,
           [:female_pop, :female_deaths] => ByRow(
               (x, y) -> y / (x + 0.5 * y)
           ) => :cond_prob2
       )
12×5 DataFrame
 Row │ age    female_deaths  female_pop  cond_prob    cond_prob2
     │ Int64  Int64          Float64     Float64      Float64
─────┼────────────────────────────────────────────────────────────
   1 │     0            136     48283.0  0.00281276   0.00281276
   2 │     1              8     47195.0  0.000169495  0.000169495
   3 │     2              3     48140.0  6.23163e-5   6.23163e-5
   4 │     3              5     50017.0  9.9961e-5    9.9961e-5
   5 │     4              2     49189.0  4.06587e-5   4.06587e-5
   6 │     5              6     49543.0  0.0001211    0.0001211
   7 │     6              6     49951.0  0.000120111  0.000120111
   8 │     7              2     49758.0  4.01937e-5   4.01937e-5
   9 │     8              2     49728.0  4.0218e-5    4.0218e-5
  10 │     9              3     49909.0  6.01076e-5   6.01076e-5
  11 │    10              2     49051.0  4.07731e-5   4.07731e-5
  12 │    11              1     49233.0  2.03114e-5   2.03114e-5

With DataFramesMeta.jl it would be:

julia> @rtransform(
           df,
           :cond_prob2 =   :female_deaths / (:female_pop + 0.5 * :female_deaths)
       )
12×5 DataFrame
 Row │ age    female_deaths  female_pop  cond_prob    cond_prob2
     │ Int64  Int64          Float64     Float64      Float64
─────┼────────────────────────────────────────────────────────────
   1 │     0            136     48283.0  0.00281276   0.00281276
   2 │     1              8     47195.0  0.000169495  0.000169495
   3 │     2              3     48140.0  6.23163e-5   6.23163e-5
   4 │     3              5     50017.0  9.9961e-5    9.9961e-5
   5 │     4              2     49189.0  4.06587e-5   4.06587e-5
   6 │     5              6     49543.0  0.0001211    0.0001211
   7 │     6              6     49951.0  0.000120111  0.000120111
   8 │     7              2     49758.0  4.01937e-5   4.01937e-5
   9 │     8              2     49728.0  4.0218e-5    4.0218e-5
  10 │     9              3     49909.0  6.01076e-5   6.01076e-5
  11 │    10              2     49051.0  4.07731e-5   4.07731e-5
  12 │    11              1     49233.0  2.03114e-5   2.03114e-5
1 Like