如何将向量中字符串的重复元素转换为R中的唯一元素?

当我们重复了字符串元素并且希望将它们用作因子水平时,这是可以的,但是如果我们要单独对待它们,则最好使每个值成为唯一元素。为此,我们可以使用make.unique函数。例如,如果我们有一个包含重复的字符串值的向量x,那么为了使它们唯一,可以使用make.unique(x)。

示例

x1<-sample(letters[1:4],10,replace=TRUE) x1

输出

[1] "a" "c" "b" "a" "d" "d" "b" "b" "c" "c"

示例

make.unique(x1)

输出

[1] "a" "b" "c" "c.1" "a.1" "b.1" "a.2" "b.2" "d" "a.3"

示例

x2<-sample(letters[1:4],120,replace=TRUE) x2

输出

[1] "d" "a" "a" "b" "a" "c" "b" "c" "b" "a" "d" "d" "b" "d" "a" "c" "a" "d"

[19] "c" "c" "c" "a" "a" "d" "c" "a" "d" "c" "c" "d" "c" "a" "d" "c" "c" "b"

[37] "a" "c" "c" "d" "d" "a" "a" "d" "d" "d" "c" "b" "d" "b" "c" "d" "a" "c"

[55] "a" "c" "d" "b" "a" "d" "d" "a" "b" "d" "b" "d" "a" "b" "b" "c" "b" "a"

[73] "d" "b" "d" "b" "c" "b" "d" "a" "c" "c" "c" "c" "b" "b" "c" "a" "d" "d"

[91] "b" "c" "b" "d" "a" "a" "d" "a" "b" "c" "c" "a" "a" "a" "c" "a" "d" "d"

[109] "d" "c" "a" "d" "d" "b" "a" "a" "a" "c" "a" "b"

示例

make.unique(x2)

输出

[1] "d" "c" "b" "c.1" "a" "d.1" "a.1" "c.2" "d.2" "c.3"

[11] "a.2" "d.3" "c.4" "a.3" "d.4" "c.5" "c.6" "c.7" "a.4" "a.5"

[21] "b.1" "c.8" "d.5" "a.6" "c.9" "b.2" "b.3" "c.10" "a.7" "a.8"

[31] "a.9" "d.6" "a.10" "d.7" "c.11" "b.4" "b.5" "b.6" "c.12" "d.8"

[41] "d.9" "b.7" "c.13" "a.11" "a.12" "d.10" "b.8" "a.13" "c.14" "a.14"

[51] "a.15" "c.15" "d.11" "d.12" "d.13" "b.9" "d.14" "d.15" "c.16" "a.16"

[61] "b.10" "d.16" "a.17" "a.18" "d.17" "b.11" "c.17" "b.12" "b.13" "c.18"

[71] "c.19" "b.14" "c.20" "a.19" "a.20" "d.18" "a.21" "a.22" "a.23" "d.19"

[81] "d.20" "c.21" "b.15" "b.16" "d.21" "b.17" "c.22" "a.24" "a.25" "b.18"

[91] "a.26" "c.23" "d.22" "d.23" "d.24" "c.24" "b.19" "a.27" "b.20" "a.28"

[101] "d.25" "a.29" "b.21" "a.30" "c.25" "b.22" "a.31" "d.26" "d.27" "d.28"

[111] "c.26" "b.23" "d.29" "b.24" "c.27" "b.25" "d.30" "a.32" "b.26" "d.31"

示例

x3<-sample(LETTERS[1:10],100,replace=TRUE) x3

输出

[1] "B" "H" "H" "E" "F" "E" "A" "I" "J" "D" "C" "A" "B" "A" "C" "J" "D" "C"

[19] "J" "G" "A" "D" "D" "F" "F" "G" "B" "F" "F" "D" "B" "I" "C" "F" "G" "F"

[37] "D" "J" "H" "C" "B" "G" "G" "E" "G" "B" "F" "A" "F" "I" "A" "J" "I" "E"

[55] "A" "H" "H" "A" "C" "J" "I" "B" "D" "I" "A" "E" "G" "E" "E" "J" "J" "G"

[73] "I" "J" "D" "I" "J" "E" "E" "G" "D" "J" "G" "E" "A" "B" "H" "B" "C" "C"

[91] "A" "J" "G" "I" "H" "J" "E" "E" "G" "C"

示例

make.unique(x3)

输出

[1] "F" "J" "C" "I" "D" "J.1" "J.2" "J.3" "I.1" "F.1"

[11] "C.1" "F.2" "I.2" "I.3" "J.4" "B" "D.1" "F.3" "H" "F.4"

[21] "B.1" "D.2" "J.5" "D.3" "B.2" "A" "B.3" "F.5" "D.4" "A.1"

[31] "D.5" "J.6" "H.1" "J.7" "B.4" "I.4" "F.6" "F.7" "A.2" "I.5"

[41] "B.5" "C.2" "J.8" "C.3" "C.4" "A.3" "D.6" "B.6" "E" "C.5"

[51] "G" "J.9" "A.4" "H.2" "I.6" "J.10" "J.11" "C.6" "H.3" "G.1"

[61] "B.7" "C.7" "A.5" "F.8" "I.7" "H.4" "J.12" "E.1" "E.2" "E.3"

[71] "A.6" "J.13" "B.8" "A.7" "H.5" "F.9" "B.9" "B.10" "J.14" "F.10"

[81] "D.7" "E.4" "A.8" "F.11" "I.8" "G.2" "B.11" "J.15" "E.5" "J.16"

[91] "E.6" "H.6" "F.12" "C.8" "B.12" "E.7" "G.3" "E.8" "G.4" "A.9"

示例

x4<-sample(c("USA","CHINA","INDIA","CANADA","RUSSIA"),50,replace=TRUE)

x4

输出

[1] "INDIA" "CHINA" "INDIA" "CANADA" "INDIA" "CANADA" "INDIA" "CANADA"

[9] "INDIA" "CANADA" "CANADA" "CHINA" "CANADA" "CANADA" "INDIA" "CANADA"

[17] "CHINA" "INDIA" "CANADA" "RUSSIA" "INDIA" "CANADA" "CANADA" "CHINA"

[25] "CANADA" "CHINA" "RUSSIA" "RUSSIA" "INDIA" "CHINA" "CANADA" "USA"

[33] "USA" "USA" "RUSSIA" "USA" "CANADA" "CHINA" "USA" "RUSSIA"

[41] "CHINA" "RUSSIA" "RUSSIA" "USA" "CHINA" "USA" "USA" "RUSSIA"

[49] "CHINA" "CANADA"

示例

x5<-sample(c("tutor","e-learning","programming","tech"),60,replace=TRUE)

x5

输出

[1] "tutor" "tutor" "programming" "tutor" "tech"

[6] "tutor" "tech" "tech" "tutor" "tech"

[11] "tutor" "tutor" "e-learning" "e-learning" "tech"

[16] "e-learning" "tutor" "e-learning" "tutor" "tech"

[21] "tutor" "programming" "e-learning" "tech" "tutor"

[26] "e-learning" "tutor" "e-learning" "tutor" "e-learning"

[31] "tech" "programming" "programming" "tutor" "programming"

[36] "tech" "tech" "programming" "programming" "programming"

[41] "e-learning" "e-learning" "e-learning" "e-learning" "tech"

[46] "tech" "programming" "tutor" "e-learning" "e-learning"

[51] "programming" "e-learning" "tutor" "e-learning" "tutor"

[56] "e-learning" "programming" "e-learning" "tutor" "programming"

示例

make.unique(x5)

输出

[1] "programming" "tech" "programming.1" "tutor"

[5] "e-learning" "programming.2" "e-learning.1" "programming.3"

[9] "tutor.1" "tech.1" "tech.2" "programming.4"

[13] "tech.3" "programming.5" "e-learning.2" "tutor.2"

[17] "programming.6" "programming.7" "e-learning.3" "tech.4"

[21] "tutor.3" "tech.5" "programming.8" "e-learning.4"

[25] "e-learning.5" "tech.6" "programming.9" "programming.10"

[29] "programming.11" "tech.7" "programming.12" "tech.8"

[33] "e-learning.6" "tutor.4" "programming.13" "e-learning.7"

[37] "e-learning.8" "tutor.5" "e-learning.9" "tutor.6"

[41] "tutor.7" "e-learning.10" "tech.9" "e-learning.11"

[45] "tutor.8" "e-learning.12" "programming.14" "e-learning.13"

[49] "e-learning.14" "programming.15" "tutor.9" "e-learning.15"

[53] "programming.16" "programming.17" "tutor.10" "tutor.11"

 [57] "tech.10" "tech.11" "programming.18" "programming.19"

以上是 如何将向量中字符串的重复元素转换为R中的唯一元素? 的全部内容, 来源链接: utcz.com/z/321739.html

回到顶部