如何从最大长度的R中的向量中提取所有字符串值?
如果我们有一个字符串向量,则向量中的所有值都不可能具有相同的大小,因此我们可能正在寻找较小的值或较大的值。因此,如果我们想从R中具有最大长度的向量中提取所有字符串值,即使存在重复项,也可以使用max和nchar函数来完成,如以下示例所示。
示例
x1<-sample(c("india","russia","egypt","canada"),100,replace=TRUE) x1输出结果
[1] "egypt" "canada" "india" "russia" "canada" "russia" "canada" "russia" [9] "egypt" "russia" "egypt" "canada" "india" "canada" "russia" "egypt" [17] "canada" "egypt" "canada" "canada" "canada" "egypt" "russia" "egypt" [25] "russia" "canada" "india" "canada" "india" "russia" "india" "india" [33] "russia" "canada" "canada" "egypt" "india" "russia" "egypt" "russia" [41] "canada" "egypt" "canada" "canada" "canada" "india" "canada" "canada" [49] "canada" "egypt" "egypt" "egypt" "india" "russia" "canada" "egypt" [57] "india" "russia" "egypt" "canada" "india" "egypt" "canada" "egypt" [65] "russia" "canada" "russia" "canada" "egypt" "india" "canada" "india" [73] "india" "canada" "india" "russia" "canada" "canada" "canada" "egypt" [81] "egypt" "canada" "russia" "canada" "egypt" "canada" "india" "egypt" [89] "canada" "egypt" "india" "canada" "india" "russia" "india" "egypt" [97] "india" "india" "india" "russia"
x1[nchar(x1)==max(nchar(x1))]
[1] "canada" "russia" "canada" "russia" "canada" "russia" "russia" "canada" [9] "canada" "russia" "canada" "canada" "canada" "canada" "russia" "russia" [17] "canada" "canada" "russia" "russia" "canada" "canada" "russia" "russia" [25] "canada" "canada" "canada" "canada" "canada" "canada" "canada" "russia" [33] "canada" "russia" "canada" "canada" "russia" "canada" "russia" "canada" [41] "canada" "canada" "russia" "canada" "canada" "canada" "canada" "russia" [49] "canada" "canada" "canada" "canada" "russia" "russia"
示例
x2<-sample(c("ABC","BCD","AC","BCD","ABD","AD","BC","CA","A","B","C"),150,replace=TRUE) x2输出结果
[1] "AC" "B" "ABD" "AC" "BC" "C" "AD" "B" "BCD" "A" "BC" "AC" [13] "BCD" "BCD" "CA" "ABC" "CA" "A" "BCD" "CA" "AC" "BCD" "BC" "BCD" [25] "BCD" "AD" "CA" "CA" "BC" "ABD" "BCD" "A" "AD" "AC" "BCD" "ABC" [37] "BCD" "BC" "B" "AD" "AC" "BCD" "BCD" "BC" "B" "CA" "AD" "AD" [49] "AC" "C" "B" "C" "ABC" "A" "CA" "A" "AD" "BCD" "C" "BC" [61] "BCD" "C" "BCD" "ABD" "BCD" "AD" "AC" "BCD" "AC" "A" "BCD" "ABD" [73] "BCD" "B" "AC" "AD" "B" "BCD" "A" "AC" "AD" "ABD" "AD" "BC" [85] "ABC" "AD" "BCD" "BCD" "AC" "C" "BCD" "AC" "AC" "BCD" "ABC" "BCD" [97] "A" "CA" "C" "BC" "CA" "C" "C" "AD" "A" "AD" "AC" "B" [109] "BCD" "CA" "ABD" "AC" "CA" "ABC" "C" "C" "B" "AD" "CA" "ABC" [121] "AD" "ABC" "A" "BCD" "A" "C" "BCD" "CA" "C" "AC" "AD" "ABD" [133] "BCD" "C" "C" "BCD" "CA" "B" "BCD" "ABD" "B" "BCD" "AC" "B" [145] "A" "A" "BC" "CA" "B" "AD"
x2[nchar(x2)==max(nchar(x2))]
[1] "ABD" "BCD" "BCD" "BCD" "ABC" "BCD" "BCD" "BCD" "BCD" "ABD" "BCD" "BCD" [13] "ABC" "BCD" "BCD" "BCD" "ABC" "BCD" "BCD" "BCD" "ABD" "BCD" "BCD" "BCD" [25] "ABD" "BCD" "BCD" "ABD" "ABC" "BCD" "BCD" "BCD" "BCD" "ABC" "BCD" "BCD" [37] "ABD" "ABC" "ABC" "ABC" "BCD" "BCD" "ABD" "BCD" "BCD" "BCD" "ABD" "BCD"
示例
x3<-sample(c("1","101","01","121","1234","02","21","41","2142","0214","3214","0124","1024","03","04","2"),150,replace=TRUE) x3输出结果
[1] "3214" "0214" "2142" "02" "0214" "1" "0124" "1234" "1024" "03" [11] "0214" "41" "04" "1234" "02" "1" "2142" "41" "21" "01" [21] "04" "121" "101" "21" "0124" "41" "3214" "1024" "0124" "3214" [31] "03" "121" "0124" "1" "41" "0214" "0214" "01" "01" "02" [41] "1234" "1234" "1" "21" "0214" "1" "121" "1234" "3214" "0214" [51] "3214" "121" "1234" "1234" "03" "101" "04" "121" "02" "1234" [61] "1" "21" "03" "0214" "101" "1" "41" "01" "0124" "2" [71] "0124" "2142" "1024" "121" "1" "1234" "03" "02" "0214" "03" [81] "04" "1024" "2142" "41" "2142" "3214" "2" "0124" "2" "21" [91] "01" "21" "21" "02" "0124" "3214" "02" "3214" "03" "04" [101] "121" "03" "21" "121" "0214" "2142" "1234" "2142" "41" "02" [111] "2" "41" "2142" "1024" "0214" "41" "1234" "2142" "03" "2142" [121] "2142" "0124" "0214" "121" "0124" "01" "3214" "21" "0124" "121" [131] "0124" "01" "02" "2" "1234" "3214" "02" "03" "2142" "1234" [141] "0124" "1024" "04" "121" "02" "2142" "2" "41" "1024" "0124"
x3[nchar(x3)==max(nchar(x3))]
[1] "3214" "0214" "2142" "0214" "0124" "1234" "1024" "0214" "1234" "2142" [11] "0124" "3214" "1024" "0124" "3214" "0124" "0214" "0214" "1234" "1234" [21] "0214" "1234" "3214" "0214" "3214" "1234" "1234" "1234" "0214" "0124" [31] "0124" "2142" "1024" "1234" "0214" "1024" "2142" "2142" "3214" "0124" [41] "0124" "3214" "3214" "0214" "2142" "1234" "2142" "2142" "1024" "0214" [51] "1234" "2142" "2142" "2142" "0124" "0214" "0124" "3214" "0124" "0124" [61] "1234" "3214" "2142" "1234" "0124" "1024" "2142" "1024" "0124"
示例
x4<-sample(c("abc","ac","ab","ca","cab","cbc","c","a"),150,replace=TRUE) x4输出结果
[1] "ab" "ab" "a" "abc" "a" "abc" "ab" "ca" "abc" "cbc" "ac" "ab" [13] "ab" "a" "ca" "abc" "ac" "abc" "abc" "cab" "abc" "cbc" "c" "cab" [25] "ac" "abc" "cbc" "abc" "ca" "abc" "a" "ca" "ab" "ab" "ab" "a" [37] "cab" "ab" "ca" "ca" "ca" "ab" "cbc" "c" "a" "a" "cab" "ac" [49] "c" "cbc" "abc" "c" "c" "cbc" "a" "ac" "cab" "abc" "ac" "ab" [61] "c" "c" "abc" "cbc" "ca" "c" "ac" "ca" "abc" "cbc" "cab" "c" [73] "ca" "abc" "a" "ac" "c" "c" "ca" "cab" "ca" "a" "abc" "cbc" [85] "abc" "ab" "c" "ab" "c" "ab" "cab" "ab" "cab" "c" "cab" "abc" [97] "a" "a" "ab" "ac" "abc" "cbc" "a" "a" "cbc" "c" "abc" "abc" [109] "ca" "ac" "cab" "cbc" "cbc" "abc" "cbc" "abc" "ac" "ca" "c" "ab" [121] "abc" "a" "c" "abc" "cbc" "a" "c" "cab" "ab" "abc" "cab" "cbc" [133] "ac" "a" "ca" "ab" "abc" "a" "abc" "c" "ca" "cab" "abc" "cbc" [145] "cbc" "c" "ca" "cbc" "abc" "a"
x4[nchar(x4)==max(nchar(x4))]
[1] "abc" "abc" "abc" "cbc" "abc" "abc" "abc" "cab" "abc" "cbc" "cab" "abc" [13] "cbc" "abc" "abc" "cab" "cbc" "cab" "cbc" "abc" "cbc" "cab" "abc" "abc" [25] "cbc" "abc" "cbc" "cab" "abc" "cab" "abc" "cbc" "abc" "cab" "cab" "cab" [37] "abc" "abc" "cbc" "cbc" "abc" "abc" "cab" "cbc" "cbc" "abc" "cbc" "abc" [49] "abc" "abc" "cbc" "cab" "abc" "cab" "cbc" "abc" "abc" "cab" "abc" "cbc" [61] "cbc" "cbc" "abc"