Home > Software engineering >  create a new variable based on other using R
create a new variable based on other using R

Time:06-29

I have a dataframe with 2 key variables and I want to create a third one based on existing one, my data:

Qi age
1 56
5 62
4 24
2 46

I want to create a variable called "general_capitale" if:

Qi == 1 "general_capitale" takes a random number between 0 and 6;

Qi == 2 "general_capitale" takes a random number between 6 and 12;

Qi == 3 "general_capitale" take either 14, 15, 17 or 20;

And 0 otherwise,

Any ideas please

CodePudding user response:

In base R:

set.seed(100)
dat$general_capitale = 0
dat$general_capitale[dat$Qi==1]  = sample(0:6, length(dat$general_capitale[dat$Qi==1]), replace = TRUE)
dat$general_capitale[dat$Qi==2]  = sample(6:12, length(dat$general_capitale[dat$Qi==2]), replace = TRUE)
dat$general_capitale[dat$Qi==3]  = sample(c(14,15,17,20), length(dat$general_capitale[dat$Qi==2]), replace = TRUE)

dat
#   Qi age general_capitale
# 1  1  56                2
# 2  5  62                0
# 3  4  24                0
# 4  2  46               12

I have assumed by "random number" you mean random integer within range from a uniform distribution.

CodePudding user response:

df$general_capitale <- (
  (df$Qi == 1) * sample(0:6, nrow(df), replace = TRUE)  
    (df$Qi == 2) * sample(6:12, nrow(df), replace = TRUE)  
    (df$Qi == 3) * sample(c(14, 15, 17, 20), nrow(df), replace = TRUE) 
)
  • Related