如何在R中的字符串向量的末尾删除空格?
有时,字符串向量中的值的末尾有多余的空格,这可能在键入值时或由于其他一些手动错误而发生。要删除字符串向量末尾的空格,我们可以使用gsub函数。例如,如果我们有一个名为x的向量,该向量的末尾包含带空格的字符串值,则可以通过使用命令gsub(“ $”,“”,x,perl = T)来删除值。
示例
x1<-sample(c("india ","croatia ","egypt "),100,replace=TRUE)输出结果x1
[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 "
示例
x1<-gsub(" $","",x1,perl=T)输出结果x1
[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"
示例
x2<-sample(c("male ","female "),100,replace=TRUE)输出结果x2
[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 "
示例
x2<-gsub(" $","",x2,perl=T)输出结果x2
[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"
示例
x3<-sample(c("hot ","cold ","warm "),100,replace=TRUE)输出结果x3
[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 "
示例
x3<-gsub(" $","",x3,perl=T)输出结果x3
[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"
示例
x4<-sample(c("Asia ","America ","Africa ","Europe ","Oceania "),100,replace=TRUE)输出结果x4
[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 "
示例
x4<-gsub(" $","",x4,perl=T)输出结果x4
[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"
以上是 如何在R中的字符串向量的末尾删除空格? 的全部内容, 来源链接: utcz.com/z/352247.html