How to remove dot and number at the end of the string in an R vector?

R ProgrammingServer Side ProgrammingProgramming

To remove dot and number at the end of the string, we can use gsub function. It will search for the pattern of dot and number at the end of the string in the vector then removal of the pattern can be done by using double quotes without space. After that the vector will be passed as shown in the below examples.

Example1

 Live Demo

x1<−c("weds.12","red.1","the.3")
x1<−gsub("\\.[0-9]*$","",x1)
x1

Output

[1] "weds" "red" "the"

Example2

 Live Demo

sample(c("india.32","russia.14","croatia.17","indonesia.54","japan.05"),80,replace=TRUE)
x2

Output

[1] "japan.05" "russia.14" "japan.05" "india.32" "japan.05"
[6] "japan.05" "japan.05" "india.32" "croatia.17" "croatia.17"
[11] "indonesia.54" "croatia.17" "indonesia.54" "russia.14" "croatia.17"
[16] "russia.14" "india.32" "russia.14" "russia.14" "russia.14"
[21] "japan.05" "croatia.17" "croatia.17" "russia.14" "russia.14"
[26] "croatia.17" "japan.05" "russia.14" "japan.05" "indonesia.54"
[31] "indonesia.54" "croatia.17" "croatia.17" "india.32" "russia.14"
[36] "russia.14" "russia.14" "russia.14" "croatia.17" "indonesia.54"
[41] "india.32" "india.32" "india.32" "russia.14" "croatia.17"
[46] "russia.14" "croatia.17" "japan.05" "india.32" "india.32"
[51] "indonesia.54" "russia.14" "indonesia.54" "croatia.17" "croatia.17"
[56] "croatia.17" "croatia.17" "indonesia.54" "india.32" "croatia.17"
[61] "india.32" "indonesia.54" "india.32" "russia.14" "india.32"
[66] "russia.14" "russia.14" "indonesia.54" "india.32" "croatia.17"
[71] "india.32" "japan.05" "croatia.17" "croatia.17" "russia.14"
[76] "india.32" "russia.14" "russia.14" "indonesia.54" "croatia.17"

Example

x2<−gsub("\\.[0−9]*$","",x2)
x2

Output

[1] "japan" "russia" "japan" "india" "japan" "japan"
[7] "japan" "india" "croatia" "croatia" "indonesia" "croatia"
[13] "indonesia" "russia" "croatia" "russia" "india" "russia"
[19] "russia" "russia" "japan" "croatia" "croatia" "russia"
[25] "russia" "croatia" "japan" "russia" "japan" "indonesia"
[31] "indonesia" "croatia" "croatia" "india" "russia" "russia"
[37] "russia" "russia" "croatia" "indonesia" "india" "india"
[43] "india" "russia" "croatia" "russia" "croatia" "japan"
[49] "india" "india" "indonesia" "russia" "indonesia" "croatia"
[55] "croatia" "croatia" "croatia" "indonesia" "india" "croatia"
[61] "india" "indonesia" "india" "russia" "india" "russia"
[67] "russia" "indonesia" "india" "croatia" "india" "japan"
[73] "croatia" "croatia" "russia" "india" "russia" "russia"
[79] "indonesia" "croatia"

Example3

 Live Demo

x3<−sample(c("hot.100","warm.78","cold.05","lukewarm.54"),80,replace=TRUE)
x3

Output

[1] "lukewarm.54" "hot.100" "warm.78" "warm.78" "hot.100"
[6] "warm.78" "hot.100" "lukewarm.54" "warm.78" "warm.78"
[11] "hot.100" "warm.78" "cold.05" "lukewarm.54" "hot.100"
[16] "hot.100" "lukewarm.54" "lukewarm.54" "lukewarm.54" "warm.78"
[21] "warm.78" "cold.05" "hot.100" "lukewarm.54" "cold.05"
[26] "warm.78" "warm.78" "lukewarm.54" "lukewarm.54" "cold.05"
[31] "cold.05" "warm.78" "lukewarm.54" "cold.05" "cold.05"
[36] "warm.78" "hot.100" "lukewarm.54" "cold.05" "cold.05"
[41] "hot.100" "warm.78" "lukewarm.54" "lukewarm.54" "lukewarm.54"
[46] "lukewarm.54" "lukewarm.54" "warm.78" "hot.100" "lukewarm.54"
[51] "lukewarm.54" "lukewarm.54" "lukewarm.54" "cold.05" "cold.05"
[56] "lukewarm.54" "warm.78" "cold.05" "hot.100" "cold.05"
[61] "warm.78" "lukewarm.54" "warm.78" "cold.05" "lukewarm.54"
[66] "lukewarm.54" "hot.100" "hot.100" "cold.05" "lukewarm.54"
[71] "cold.05" "lukewarm.54" "warm.78" "cold.05" "cold.05"
[76] "warm.78" "lukewarm.54" "cold.05" "warm.78" "cold.05"

Example

x3<−gsub("\\.[0−9]*$","",x3)
x3

Output

[1] "lukewarm" "hot" "warm" "warm" "hot" "warm"
[7] "hot" "lukewarm" "warm" "warm" "hot" "warm"
[13] "cold" "lukewarm" "hot" "hot" "lukewarm" "lukewarm"
[19] "lukewarm" "warm" "warm" "cold" "hot" "lukewarm"
[25] "cold" "warm" "warm" "lukewarm" "lukewarm" "cold"
[31] "cold" "warm" "lukewarm" "cold" "cold" "warm"
[37] "hot" "lukewarm" "cold" "cold" "hot" "warm"
[43] "lukewarm" "lukewarm" "lukewarm" "lukewarm" "lukewarm" "warm"
[49] "hot" "lukewarm" "lukewarm" "lukewarm" "lukewarm" "cold"
[55] "cold" "lukewarm" "warm" "cold" "hot" "cold"
[61] "warm" "lukewarm" "warm" "cold" "lukewarm" "lukewarm"
[67] "hot" "hot" "cold" "lukewarm" "cold" "lukewarm"
[73] "warm" "cold" "cold" "warm" "lukewarm" "cold"
[79] "warm" "cold"

Example4

 Live Demo

x4<−sample(c("Male.1","Female.2"),80,replace=TRUE)
x4

Output

[1] "Female.2" "Male.1" "Male.1" "Male.1" "Male.1" "Female.2"
[7] "Male.1" "Male.1" "Female.2" "Male.1" "Female.2" "Female.2"
[13] "Male.1" "Female.2" "Female.2" "Female.2" "Male.1" "Female.2"
[19] "Male.1" "Male.1" "Male.1" "Female.2" "Female.2" "Female.2"
[25] "Female.2" "Female.2" "Male.1" "Female.2" "Male.1" "Female.2"
[31] "Female.2" "Male.1" "Female.2" "Male.1" "Male.1" "Male.1"
[37] "Female.2" "Female.2" "Female.2" "Female.2" "Female.2" "Female.2"
[43] "Male.1" "Female.2" "Female.2" "Male.1" "Male.1" "Female.2"
[49] "Female.2" "Female.2" "Female.2" "Female.2" "Male.1" "Female.2"
[55] "Female.2" "Female.2" "Female.2" "Female.2" "Male.1" "Female.2"
[61] "Female.2" "Female.2" "Male.1" "Male.1" "Female.2" "Male.1"
[67] "Male.1" "Female.2" "Male.1" "Male.1" "Female.2" "Male.1"
[73] "Female.2" "Male.1" "Female.2" "Female.2" "Male.1" "Male.1"
[79] "Female.2" "Male.1"

Example

x4<−gsub("\\.[0−9]*$","",x4)
x4

Output

[1] "Female" "Male" "Male" "Male" "Male" "Female" "Male" "Male"
[9] "Female" "Male" "Female" "Female" "Male" "Female" "Female" "Female"
[17] "Male" "Female" "Male" "Male" "Male" "Female" "Female" "Female"
[25] "Female" "Female" "Male" "Female" "Male" "Female" "Female" "Male"
[33] "Female" "Male" "Male" "Male" "Female" "Female" "Female" "Female"
[41] "Female" "Female" "Male" "Female" "Female" "Male" "Male" "Female"
[49] "Female" "Female" "Female" "Female" "Male" "Female" "Female" "Female"
[57] "Female" "Female" "Male" "Female" "Female" "Female" "Male" "Male"
[65] "Female" "Male" "Male" "Female" "Male" "Male" "Female" "Male"
[73] "Female" "Male" "Female" "Female" "Male" "Male" "Female" "Male"
raja
Published on 10-Feb-2021 07:14:49
Advertisements