king_arrival27
king_arrival27

Reputation: 89

how to create my own formula with specific variable

I have a dataset with 4 columns and want to apply my own formula for the dataset with specific column condition

the dataset is

s.no  id  sex  age  cre
1   103469  M   68  0.9
2   103469  M   68  NULL
3   103469  F   68  NULL
4   103469  F    68 0.8
5   103469  M   68  0.8
6   103469  M   69  0.9

the formula i want to construct would be A*(cre/B)^C*0.993^age)

where

A = 150
B = 20
C = 29

I want to apply this formula if column sex = "M" and cre <0.9.

Upvotes: 1

Views: 54

Answers (1)

Ronak Shah
Ronak Shah

Reputation: 389275

In base R, we can use transform with ifelse assuming df is the dataframe name

transform(df,new_form = ifelse(sex == "M" & cre < 0.9, A*(cre/B)^C*0.993^age, NA))

Or using data.table

library(data.table)
setDT(df)[sex == "M" & cre < 0.9, formula := A*(cre/B)^C*0.993^age]

Upvotes: 2

Related Questions