Conditional Assignment frame$col <- val with magrittr

Viewed 79

What is the Magrittr syntax to assign Iris$column based on Iris$Petal.Length ? Example without Magrittr:

df      <- iris[47:56,]
df$val1 <- NA                                           ## create column
df$val1[which(df$Petal.Length < 1.52)]                         <- "cake"
df$val1[which(df$Petal.Length > 1.55 & df$Petal.Length <=4.55)] <- "pie"
df$val1[which(df$Petal.Length > 4.55)]                        <- "apple"

head(df)

This results in:

Petal.Length Petal.Width  Species    val1

1.6                0.2     setosa     pie

1.4                0.2     setosa     cake

1.5                0.2     setosa     cake

1.4                0.2     setosa     cake

1.4                1.4.  versicolor   apple
2 Answers
Related