mercredi 13 mai 2020

Column repeating row values before TRUE values

I have a simple question. I am trying to create a column where it repeats the value before TRUE values of grepl. So, in a data.frame like that: df <- data.frame(name = c("E11","M05","M05","E12","M05","E13","E14"))

I was trying to use something like this unsuccessfully: df %>% mutate( clone = ifelse(grepl("M05",name), lag(name), ""))

Here is the output I expect:

| name | clone |
+------+-------+
| E11  |       |
| M05  | E11   |
| M05  | E11   |
| E12  |       |
| M05  | E12   |
| E13  |       |
| E14  |       |
+------+-------+


Aucun commentaire:

Enregistrer un commentaire