lundi 22 mai 2017

replace string with a random character from a selection

How can you take the string and replace every instance of “.”, “,”, " " (i.e. dot, comma or space) with one random character selected from c(‘|’,‘:’,‘@’,’*')

Say i have a string like this

 Aenean ut odio dignissim augue rutrum faucibus. Fusce posuere, tellus eget viverra mattis, erat tellus porta mi, at facilisis sem nibh non urna. Phasellus quis turpis quis mauris suscipit vulputate. Sed interdum lacus non velit. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae;

To get one random character, we can treat the characters as a vector then use sample function to select one out. I assume first i need to search for dot, comma or space, then use gsub functino to replace all these?




Aucun commentaire:

Enregistrer un commentaire