mardi 26 novembre 2019

Assign value to variables in a loop with some conditions in R

I would like to create a loop to

  1. create a serie of variables a1-a17 to a given dataframe (df)
  2. assign values of variable C to a1-a17 based on the value of variable b.

I don't want to do this 17 times, please give me some advices to make them into a loop:

df$a1 <- ifelse(df$b ==1, df$c, NA )
df$a2 <- ifelse(df$b ==2, df$c, NA )
.
.
.
df$a16 <- ifelse(df$b ==16, df$c, NA )
df$a17 <- ifelse(df$b ==17 , df$c, NA )

thanks for any answers for a beginner!

Aucun commentaire:

Enregistrer un commentaire