Home > Back-end >  create a new variable based on other factors using R
create a new variable based on other factors using R

Time:06-29

So I have this dataframe and I aim to add a new variable based on others:

Qi Age c_gen
1 56 13
2 43 15
5 31 6
3 67 8

I want to create a variable called c_sep that if:

Qi==1 or Qi==2 c_sep takes a random number between (c_gen 6) and Age;

Qi==3 or Qi==4 c_sep takes a random number between (Age-15) and Age;

And 0 otherwise,

so my data would look something like this:

Qi Age c_gen c_sep
1 56 13 24
2 43 15 13
5 31 6 0
3 67 8 40

Any ideas please

CodePudding user response:

In base R, you can do something along the lines of:

dat  <- read.table(text = "Qi   Age c_gen
1   56  13
2   43  15
5   31  6
3   67  8", header = T)

set.seed(100)
dat$c_sep  <- 0
dat$c_sep[dat$Qi %in% c(1,2)]  <- apply(dat[dat$Qi %in% c(1,2),], 1, \(row) sample(
    (row["c_gen"] 6):row["Age"], 1
    )
)
dat$c_sep[dat$Qi %in% c(3,4)]  <- apply(dat[dat$Qi %in% c(3,4),], 1, \(row) sample(
    (row["Age"]-15):row["Age"], 1
    )
)
dat
#   Qi Age c_gen c_sep
# 1  1  56    13    28
# 2  2  43    15    43
# 3  5  31     6     0
# 4  3  67     8    57

If you are doing it more than twice you might want to put this in a function - depending on your requirements.

CodePudding user response:

Try this

df$c_sep <- ifelse(df$Qi == 1 | df$Qi == 2 ,
sapply(1:nrow(df) ,
\(x) sample(seq(df$c_gen[x]   6, df$Age[x]) ,1)) ,
sapply(1:nrow(df) ,
\(x) sample(seq(df$Age[x] - 15, df$Age[x]) ,1)) , 0))
  • output
  Qi Age c_gen c_sep
1  1  56    13    41
2  2  43    15    42
3  5  31     6     0
4  3  67     8    58

CodePudding user response:

A tidyverse option:

library(tidyverse)

df <- tribble(
  ~Qi, ~Age, ~c_gen,
  1, 56, 13,
  2, 43, 15,
  5, 31, 6,
  3, 67, 8
)

df |> 
  rowwise() |> 
  mutate(c_sep = case_when(
    Qi <= 2           ~ sample(seq(c_gen   6, Age, 1), 1),
    between(Qi, 3, 4) ~ sample(seq(Age - 15, Age, 1), 1),
    TRUE              ~ 0
  )) |> 
  ungroup()

#> # A tibble: 4 × 4
#>      Qi   Age c_gen c_sep
#>   <dbl> <dbl> <dbl> <dbl>
#> 1     1    56    13    39
#> 2     2    43    15    41
#> 3     5    31     6     0
#> 4     3    67     8    54

Created on 2022-06-29 by the reprex package (v2.0.1)

  • Related