How to remove spaces at the end in string vectors in R?

R ProgrammingServer Side ProgrammingProgramming

Sometimes values in a string vector has an extra space at the end, this might happen while typing the values or due to some other manual errors. To remove spaces at the end in string vectors, we can use gsub function. For example, if we have a vector called x that contains string values with spaces at the end then the removal of values can be done by using the command gsub(" $","",x,perl=T)

Example

 Live Demo

x1<-sample(c("india ","croatia ","egypt "),100,replace=TRUE)
x1

Output

[1]  "croatia " "india " "india " "india " "croatia " "india "
[7]  "india " "egypt " "croatia " "egypt " "croatia " "croatia "
[13] "croatia " "croatia " "croatia " "croatia " "india " "egypt "
[19] "india " "croatia " "croatia " "croatia " "croatia " "croatia "
[25] "croatia " "croatia " "india " "india " "egypt " "croatia "
[31] "india " "croatia " "egypt " "egypt " "croatia " "croatia "
[37] "croatia " "croatia " "croatia " "india " "croatia " "croatia "
[43] "croatia " "egypt " "india " "india " "india " "croatia "
[49] "egypt " "egypt " "croatia " "croatia " "croatia " "egypt "
[55] "egypt " "india " "egypt " "egypt " "egypt " "egypt "
[61] "croatia " "india " "egypt " "india " "india " "croatia "
[67] "india " "egypt " "croatia " "india " "egypt " "egypt "
[73] "croatia " "egypt " "india " "india " "croatia " "egypt "
[79] "egypt " "india " "egypt " "croatia " "egypt " "india "
[85] "croatia " "india " "india " "india " "india " "india "
[91] "croatia " "croatia " "egypt " "egypt " "india " "egypt "
[97] "egypt " "croatia " "croatia " "croatia "

Example

x1<-gsub(" $","",x1,perl=T)
x1

Output

[1] "croatia" "india" "india" "india" "croatia" "india" "india"
[8] "egypt" "croatia" "egypt" "croatia" "croatia" "croatia" "croatia"
[15] "croatia" "croatia" "india" "egypt" "india" "croatia" "croatia"
[22] "croatia" "croatia" "croatia" "croatia" "croatia" "india" "india"
[29] "egypt" "croatia" "india" "croatia" "egypt" "egypt" "croatia"
[36] "croatia" "croatia" "croatia" "croatia" "india" "croatia" "croatia"
[43] "croatia" "egypt" "india" "india" "india" "croatia" "egypt"
[50] "egypt" "croatia" "croatia" "croatia" "egypt" "egypt" "india"
[57] "egypt" "egypt" "egypt" "egypt" "croatia" "india" "egypt"
[64] "india" "india" "croatia" "india" "egypt" "croatia" "india"
[71] "egypt" "egypt" "croatia" "egypt" "india" "india" "croatia"
[78] "egypt" "egypt" "india" "egypt" "croatia" "egypt" "india"
[85] "croatia" "india" "india" "india" "india" "india" "croatia"
[92] "croatia" "egypt" "egypt" "india" "egypt" "egypt" "croatia"
[99] "croatia" "croatia"

Example

 Live Demo

x2<-sample(c("male ","female "),100,replace=TRUE)
x2

Output

[1] "female " "female " "male " "male " "male " "female " "male "
[8] "female " "female " "male " "male " "male " "female " "male "
[15] "male " "female " "female " "female " "male " "male " "male "
[22] "female " "female " "male " "female " "male " "female " "male "
[29] "female " "female " "female " "male " "male " "female " "female "
[36] "male " "male " "female " "female " "male " "female " "male "
[43] "male " "male " "male " "male " "male " "female " "male "
[50] "female " "male " "female " "male " "female " "male " "male "
[57] "male " "female " "male " "female " "female " "male " "female "
[64] "male " "male " "male " "female " "female " "male " "female "
[71] "female " "female " "female " "female " "male " "female " "male "
[78] "female " "male " "female " "male " "male " "male " "female "
[85] "male " "female " "male " "male " "female " "male " "male "
[92] "female " "male " "female " "male " "male " "female " "male "
[99] "male " "male "

Example

x2<-gsub(" $","",x2,perl=T)
x2

Output

[1] "female" "female" "male" "male" "male" "female" "male" "female"
[9] "female" "male" "male" "male" "female" "male" "male" "female"
[17] "female" "female" "male" "male" "male" "female" "female" "male"
[25] "female" "male" "female" "male" "female" "female" "female" "male"
[33] "male" "female" "female" "male" "male" "female" "female" "male"
[41] "female" "male" "male" "male" "male" "male" "male" "female"
[49] "male" "female" "male" "female" "male" "female" "male" "male"
[57] "male" "female" "male" "female" "female" "male" "female" "male"
[65] "male" "male" "female" "female" "male" "female" "female" "female"
[73] "female" "female" "male" "female" "male" "female" "male" "female"
[81] "male" "male" "male" "female" "male" "female" "male" "male"
[89] "female" "male" "male" "female" "male" "female" "male" "male"
[97] "female" "male" "male" "male"

Example

 Live Demo

x3<-sample(c("hot ","cold ","warm "),100,replace=TRUE)
x3

Output

[1] "cold " "hot " "hot " "warm " "hot " "hot " "hot " "hot " "hot "
[10] "hot " "hot " "cold " "hot " "cold " "cold " "cold " "hot " "cold "
[19] "cold " "cold " "cold " "cold " "cold " "hot " "warm " "warm " "hot "
[28] "warm " "hot " "hot " "hot " "warm " "cold " "cold " "cold " "hot "
[37] "warm " "cold " "hot " "cold " "hot " "cold " "cold " "cold " "hot "
[46] "cold " "warm " "cold " "warm " "cold " "warm " "cold " "warm " "warm "
[55] "cold " "hot " "cold " "warm " "warm " "cold " "cold " "cold " "hot "
[64] "warm " "cold " "hot " "cold " "cold " "warm " "warm " "hot " "warm "
[73] "cold " "warm " "cold " "warm " "hot " "warm " "cold " "hot " "hot "
[82] "cold " "warm " "cold " "cold " "hot " "cold " "hot " "hot " "cold "
[91] "cold " "hot " "cold " "cold " "cold " "warm " "hot " "warm " "cold "
[100] "hot "

Example

x3<-gsub(" $","",x3,perl=T)
x3

Output

[1] "cold" "hot" "hot" "warm" "hot" "hot" "hot" "hot" "hot" "hot"
[11] "hot" "cold" "hot" "cold" "cold" "cold" "hot" "cold" "cold" "cold"
[21] "cold" "cold" "cold" "hot" "warm" "warm" "hot" "warm" "hot" "hot"
[31] "hot" "warm" "cold" "cold" "cold" "hot" "warm" "cold" "hot" "cold"
[41] "hot" "cold" "cold" "cold" "hot" "cold" "warm" "cold" "warm" "cold"
[51] "warm" "cold" "warm" "warm" "cold" "hot" "cold" "warm" "warm" "cold"
[61] "cold" "cold" "hot" "warm" "cold" "hot" "cold" "cold" "warm" "warm"
[71] "hot" "warm" "cold" "warm" "cold" "warm" "hot" "warm" "cold" "hot"
[81] "hot" "cold" "warm" "cold" "cold" "hot" "cold" "hot" "hot" "cold"
[91] "cold" "hot" "cold" "cold" "cold" "warm" "hot" "warm" "cold" "hot"

Example

 Live Demo

x4<-sample(c("Asia ","America ","Africa ","Europe ","Oceania "),100,replace=TRUE)
x4

Output

[1] "Europe " "Europe " "Oceania " "America " "Africa " "Europe "
[7] "America " "Asia " "Europe " "Africa " "Asia " "Asia "
[13] "Oceania " "Asia " "Oceania " "Europe " "Europe " "America "
[19] "Oceania " "Europe " "Asia " "Europe " "Europe " "America "
[25] "Asia " "Asia " "Oceania " "Africa " "Oceania " "Asia "
[31] "Africa " "Africa " "Oceania " "Oceania " "Oceania " "Africa "
[37] "Africa " "Africa " "America " "America " "Asia " "Africa "
[43] "Africa " "Oceania " "Asia " "Oceania " "Europe " "Europe "
[49] "Asia " "Oceania " "Africa " "America " "Europe " "Europe "
[55] "Europe " "Asia " "Europe " "Oceania " "Oceania " "Oceania "
[61] "Oceania " "Europe " "Oceania " "America " "Europe " "America "
[67] "America " "Europe " "Europe " "Africa " "Asia " "Asia "
[73] "Asia " "Europe " "America " "Europe " "America " "Oceania "
[79] "Europe " "Africa " "Asia " "America " "Asia " "America "
[85] "Europe " "Europe " "Oceania " "Europe " "Asia " "America "
[91] "Asia " "Asia " "Oceania " "Oceania " "Asia " "Asia "
[97] "Asia " "Asia " "Europe " "Oceania "

Example

x4<-gsub(" $","",x4,perl=T)
x4

Output

[1] "Europe" "Europe" "Oceania" "America" "Africa" "Europe" "America"
[8] "Asia" "Europe" "Africa" "Asia" "Asia" "Oceania" "Asia"
[15] "Oceania" "Europe" "Europe" "America" "Oceania" "Europe" "Asia"
[22] "Europe" "Europe" "America" "Asia" "Asia" "Oceania" "Africa"
[29] "Oceania" "Asia" "Africa" "Africa" "Oceania" "Oceania" "Oceania"
[36] "Africa" "Africa" "Africa" "America" "America" "Asia" "Africa"
[43] "Africa" "Oceania" "Asia" "Oceania" "Europe" "Europe" "Asia"
[50] "Oceania" "Africa" "America" "Europe" "Europe" "Europe" "Asia"
[57] "Europe" "Oceania" "Oceania" "Oceania" "Oceania" "Europe" "Oceania"
[64] "America" "Europe" "America" "America" "Europe" "Europe" "Africa"
[71] "Asia" "Asia" "Asia" "Europe" "America" "Europe" "America"
[78] "Oceania" "Europe" "Africa" "Asia" "America" "Asia" "America"
[85] "Europe" "Europe" "Oceania" "Europe" "Asia" "America" "Asia"
[92] "Asia" "Oceania" "Oceania" "Asia" "Asia" "Asia" "Asia"
[99] "Europe" "Oceania"
raja
Published on 10-Feb-2021 10:56:54
Advertisements