Sebastian Zeki
Sebastian Zeki

Reputation: 6874

Find a replace with a varying number of elements from a list

Aim

To replace strings within a dataframe column containing a certain word with words from a list. I want to replace the word with anywhere between 1 and 5 elements from the list (number of replacements is random)

Input

I have a dataframe as follows

out<-data.frame(c("Hiatus","Hiatus","Normal","Really bad","Hiatus","Abnormal"))
names(out)<-"Finding"
out$Finding<-as.character(out$Finding)

    Finding
1     Hiatus
2     Hiatus
3     Normal
4 Really bad
5     Hiatus
6   Abnormal

FD_Hiatus<-list(x="Linear",
                      x="Several",
                      x="Pull",
                      x="Crepe",
                      x="Rings",
                      x="Strictures")

Desired output (or variation thereof)

    Finding
1     Linear Several Pull
2     Pull
3     Normal
4     Really bad
5     Crepe Strictures Several
6     Abnormal

Current solution

out$Finding[out$Finding == "Hiatus"] <- sample(FD_Hiatus, sum(out$Finding == "Hiatus"), TRUE)

Current output:

     Finding
1      Crepe
2      Rings
3     Normal
4 Really bad
5     Linear
6   Abnormal

Upvotes: 0

Views: 31

Answers (1)

missuse
missuse

Reputation: 19716

Here is an appraoch:

set.seed(1)
out$Finding[out$Finding == "Hiatus"] <- unlist(lapply(1:sum(out$Finding == "Hiatus"), function(x){
  paste(sample(unlist(FD_Hiatus), size = sample(1:5)), collapse =" ")
  }
  )
)
#output
> out
                         Finding
1               Strictures Rings
2 Rings Several Crepe Strictures
3                         Normal
4                     Really bad
5     Several Crepe Linear Rings
6                       Abnormal

with seed 2:

> out
                         Finding
1                     Strictures
2                        Several
3                         Normal
4                     Really bad
5 Several Pull Linear Strictures
6                       Abnormal

Upvotes: 2

Related Questions