mercredi 3 octobre 2018

mutate column based on conditions, preserving original content

How would I go about setting a conditional mutate on a column, but preserving the original content.

For example, consider:

DF=data.frame(A=1:10, B=c("fail1", "fail2", rep(NA,8)))

DF=DF %>% mutate(B = ifelse(A<4,paste(B, "less than 4"),B))

What I want is to produce the following:

DF_want=data.frame(A=1:10, B=c("fail1 less than 4", "fail2 less than 4", "less than 4" rep(NA,7)))

But because I am calling the column B directly, and not the specific row in B, this isn't happening.

Aucun commentaire:

Enregistrer un commentaire