How to replace a sub-string with the reverse of that sub-string in R?

R ProgrammingServer Side ProgrammingProgramming

The chartr function in base R helps us to replace old strings with new strings and hence it can be also used to replace a subs-string with the reverse of that substring. For example, if we have a vector say x that contains tutorialpsoint and we want to convert it to tutorialspoint then it can be done as chartr("tutorialpsoint "," tutorialspoint ",x).

Example1

Live Demo

> x1<-"IDNIA"
> x1

Output

[1] "IDNIA"

Example

> chartr("DN","ND",x1)

Output

[1] "INDIA"

Example2

Live Demo

> x2<-sample(c("IDNIA","IDNONESIA"),50,,replace=TRUE)
> x2

Output

[1] "IDNIA" "IDNIA" "IDNIA" "IDNONESIA" "IDNIA" "IDNONESIA"
[7] "IDNONESIA" "IDNIA" "IDNONESIA" "IDNIA" "IDNIA" "IDNONESIA"
[13] "IDNONESIA" "IDNONESIA" "IDNIA" "IDNONESIA" "IDNIA" "IDNIA"
[19] "IDNONESIA" "IDNONESIA" "IDNIA" "IDNONESIA" "IDNIA" "IDNIA"
[25] "IDNONESIA" "IDNIA" "IDNIA" "IDNIA" "IDNONESIA" "IDNONESIA"
[31] "IDNONESIA" "IDNONESIA" "IDNONESIA" "IDNONESIA" "IDNIA" "IDNIA"
[37] "IDNIA" "IDNONESIA" "IDNIA" "IDNIA" "IDNONESIA" "IDNIA"
[43] "IDNONESIA" "IDNIA" "IDNONESIA" "IDNONESIA" "IDNONESIA" "IDNIA"
[49] "IDNONESIA" "IDNONESIA"

Example

> chartr("DN","ND",x2)

Output

[1] "INDIA" "INDIA" "INDIA" "INDODESIA" "INDIA" "INDODESIA"
[7] "INDODESIA" "INDIA" "INDODESIA" "INDIA" "INDIA" "INDODESIA"
[13] "INDODESIA" "INDODESIA" "INDIA" "INDODESIA" "INDIA" "INDIA"
[19] "INDODESIA" "INDODESIA" "INDIA" "INDODESIA" "INDIA" "INDIA"
[25] "INDODESIA" "INDIA" "INDIA" "INDIA" "INDODESIA" "INDODESIA"
[31] "INDODESIA" "INDODESIA" "INDODESIA" "INDODESIA" "INDIA" "INDIA"
[37] "INDIA" "INDODESIA" "INDIA" "INDIA" "INDODESIA" "INDIA"
[43] "INDODESIA" "INDIA" "INDODESIA" "INDODESIA" "INDODESIA" "INDIA"
[49] "INDODESIA" "INDODESIA"

Example3

Live Demo

> x3<-sample(c("Litre","Metre","Millimitre","Millilitre"),120,,replace=TRUE)
> x3

Output

[1] "Litre" "Millimitre" "Metre" "Metre" "Litre"
[6] "Metre" "Millilitre" "Litre" "Millimitre" "Millimitre"
[11] "Millilitre" "Millilitre" "Millilitre" "Litre" "Litre"
[16] "Millilitre" "Millilitre" "Litre" "Litre" "Millilitre"
[21] "Metre" "Millimitre" "Metre" "Millimitre" "Millilitre"
[26] "Millimitre" "Millimitre" "Metre" "Millimitre" "Metre"
[31] "Millimitre" "Millimitre" "Millimitre" "Millilitre" "Millimitre"
[36] "Millilitre" "Millilitre" "Millilitre" "Metre" "Litre"
[41] "Millimitre" "Millilitre" "Litre" "Litre" "Metre"
[46] "Litre" "Millimitre" "Metre" "Metre" "Metre"
[51] "Millimitre" "Litre" "Millilitre" "Millimitre" "Millimitre"
[56] "Litre" "Litre" "Millilitre" "Metre" "Millimitre"
[61] "Metre" "Litre" "Millimitre" "Metre" "Metre"
[66] "Millilitre" "Metre" "Litre" "Millimitre" "Litre"
[71] "Metre" "Millimitre" "Millilitre" "Millimitre" "Millimitre"
[76] "Metre" "Metre" "Litre" "Millimitre" "Litre"
[81] "Litre" "Millilitre" "Litre" "Litre" "Litre"
[86] "Millimitre" "Litre" "Litre" "Metre" "Litre"
[91] "Litre" "Litre" "Litre" "Metre" "Metre"
[96] "Millimitre" "Metre" "Litre" "Millilitre" "Millimitre"
[101] "Millilitre" "Millimitre" "Millimitre" "Metre" "Millilitre"
[106] "Millimitre" "Litre" "Millilitre" "Litre" "Millilitre"
[111] "Metre" "Millilitre" "Millimitre" "Millilitre" "Millimitre"
[116] "Litre" "Litre" "Millilitre" "Millimitre" "Litre"

Example

> chartr("re","er",x3)

Output

[1] "Liter" "Millimiter" "Mrter" "Mrter" "Liter"
[6] "Mrter" "Milliliter" "Liter" "Millimiter" "Millimiter"
[11] "Milliliter" "Milliliter" "Milliliter" "Liter" "Liter"
[16] "Milliliter" "Milliliter" "Liter" "Liter" "Milliliter"
[21] "Mrter" "Millimiter" "Mrter" "Millimiter" "Milliliter"
[26] "Millimiter" "Millimiter" "Mrter" "Millimiter" "Mrter"
[31] "Millimiter" "Millimiter" "Millimiter" "Milliliter" "Millimiter"
[36] "Milliliter" "Milliliter" "Milliliter" "Mrter" "Liter"
[41] "Millimiter" "Milliliter" "Liter" "Liter" "Mrter"
[46] "Liter" "Millimiter" "Mrter" "Mrter" "Mrter"
[51] "Millimiter" "Liter" "Milliliter" "Millimiter" "Millimiter"
[56] "Liter" "Liter" "Milliliter" "Mrter" "Millimiter"
[61] "Mrter" "Liter" "Millimiter" "Mrter" "Mrter"
[66] "Milliliter" "Mrter" "Liter" "Millimiter" "Liter"
[71] "Mrter" "Millimiter" "Milliliter" "Millimiter" "Millimiter"
[76] "Mrter" "Mrter" "Liter" "Millimiter" "Liter"
[81] "Liter" "Milliliter" "Liter" "Liter" "Liter"
[86] "Millimiter" "Liter" "Liter" "Mrter" "Liter"
[91] "Liter" "Liter" "Liter" "Mrter" "Mrter"
[96] "Millimiter" "Mrter" "Liter" "Milliliter" "Millimiter"
[101] "Milliliter" "Millimiter" "Millimiter" "Mrter" "Milliliter"
[106] "Millimiter" "Liter" "Milliliter" "Liter" "Milliliter"
[111] "Mrter" "Milliliter" "Millimiter" "Milliliter" "Millimiter"
[116] "Liter" "Liter" "Milliliter" "Millimiter" "Liter"

Example4

Live Demo

> x4<-sample(c("afterglwo","deathblwo","alpenglwo"),80,,replace=TRUE)
> x4

Output

[1] "deathblwo" "afterglwo" "alpenglwo" "deathblwo" "deathblwo" "deathblwo"
[7] "afterglwo" "afterglwo" "alpenglwo" "afterglwo" "deathblwo" "afterglwo"
[13] "deathblwo" "deathblwo" "afterglwo" "deathblwo" "afterglwo" "afterglwo"
[19] "afterglwo" "alpenglwo" "afterglwo" "deathblwo" "afterglwo" "deathblwo"
[25] "deathblwo" "deathblwo" "alpenglwo" "afterglwo" "afterglwo" "afterglwo"
[31] "deathblwo" "alpenglwo" "deathblwo" "afterglwo" "alpenglwo" "deathblwo"
[37] "deathblwo" "deathblwo" "alpenglwo" "afterglwo" "afterglwo" "afterglwo"
[43] "deathblwo" "deathblwo" "deathblwo" "afterglwo" "deathblwo" "afterglwo"
[49] "afterglwo" "afterglwo" "deathblwo" "deathblwo" "alpenglwo" "afterglwo"
[55] "afterglwo" "alpenglwo" "deathblwo" "afterglwo" "deathblwo" "deathblwo"
[61] "deathblwo" "deathblwo" "deathblwo" "deathblwo" "deathblwo" "afterglwo"
[67] "deathblwo" "afterglwo" "alpenglwo" "afterglwo" "deathblwo" "afterglwo"
[73] "afterglwo" "alpenglwo" "afterglwo" "alpenglwo" "alpenglwo" "deathblwo"
[79] "alpenglwo" "afterglwo"

Example

> chartr("ow","wo",x4)

Output

[1] "deathblow" "afterglow" "alpenglow" "deathblow" "deathblow" "deathblow"
[7] "afterglow" "afterglow" "alpenglow" "afterglow" "deathblow" "afterglow"
[13] "deathblow" "deathblow" "afterglow" "deathblow" "afterglow" "afterglow"
[19] "afterglow" "alpenglow" "afterglow" "deathblow" "afterglow" "deathblow"
[25] "deathblow" "deathblow" "alpenglow" "afterglow" "afterglow" "afterglow"
[31] "deathblow" "alpenglow" "deathblow" "afterglow" "alpenglow" "deathblow"
[37] "deathblow" "deathblow" "alpenglow" "afterglow" "afterglow" "afterglow"
[43] "deathblow" "deathblow" "deathblow" "afterglow" "deathblow" "afterglow"
[49] "afterglow" "afterglow" "deathblow" "deathblow" "alpenglow" "afterglow"
[55] "afterglow" "alpenglow" "deathblow" "afterglow" "deathblow" "deathblow"
[61] "deathblow" "deathblow" "deathblow" "deathblow" "deathblow" "afterglow"
[67] "deathblow" "afterglow" "alpenglow" "afterglow" "deathblow" "afterglow"
[73] "afterglow" "alpenglow" "afterglow" "alpenglow" "alpenglow" "deathblow"
[79] "alpenglow" "afterglow"
raja
Updated on 21-Nov-2020 05:48:52

Advertisements