我的答案在這裏,但如果有更優雅的方式,我期待着它
http://rpubs.com/newajay/uniqueidsfrom3strings
states=c("UP","MP","Raj","Maha")
districts=c("North","West","East","South")
villages=c("1","2","3","4")
newlist=NULL
for (i in 1:length(states)){
newlist=c(newlist,paste(states[i],"_",districts))
}
newlist=gsub(" ","",newlist)
newlist
[1] "UP_North" "UP_West" "UP_East" "UP_South" "MP_North"
[6] "MP_West" "MP_East" "MP_South" "Raj_North" "Raj_West"
[11] "Raj_East" "Raj_South" "Maha_North" "Maha_West" "Maha_East"
[16] "Maha_South"
for (i in 1:length(newlist)){
newlist=c(newlist,paste(newlist[i],"_",villages))
}
newlist
[1] "UP_North" "UP_West" "UP_East" "UP_South"
[5] "MP_North" "MP_West" "MP_East" "MP_South"
[9] "Raj_North" "Raj_West" "Raj_East" "Raj_South"
[13] "Maha_North" "Maha_West" "Maha_East" "Maha_South"
[17] "UP_North _ 1" "UP_North _ 2" "UP_North _ 3" "UP_North _ 4"
[21] "UP_West _ 1" "UP_West _ 2" "UP_West _ 3" "UP_West _ 4"
[25] "UP_East _ 1" "UP_East _ 2" "UP_East _ 3" "UP_East _ 4"
[29] "UP_South _ 1" "UP_South _ 2" "UP_South _ 3" "UP_South _ 4"
[33] "MP_North _ 1" "MP_North _ 2" "MP_North _ 3" "MP_North _ 4"
[37] "MP_West _ 1" "MP_West _ 2" "MP_West _ 3" "MP_West _ 4"
[41] "MP_East _ 1" "MP_East _ 2" "MP_East _ 3" "MP_East _ 4"
[45] "MP_South _ 1" "MP_South _ 2" "MP_South _ 3" "MP_South _ 4"
[49] "Raj_North _ 1" "Raj_North _ 2" "Raj_North _ 3" "Raj_North _ 4"
[53] "Raj_West _ 1" "Raj_West _ 2" "Raj_West _ 3" "Raj_West _ 4"
[57] "Raj_East _ 1" "Raj_East _ 2" "Raj_East _ 3" "Raj_East _ 4"
[61] "Raj_South _ 1" "Raj_South _ 2" "Raj_South _ 3" "Raj_South _ 4"
[65] "Maha_North _ 1" "Maha_North _ 2" "Maha_North _ 3" "Maha_North _ 4"
[69] "Maha_West _ 1" "Maha_West _ 2" "Maha_West _ 3" "Maha_West _ 4"
[73] "Maha_East _ 1" "Maha_East _ 2" "Maha_East _ 3" "Maha_East _ 4"
[77] "Maha_South _ 1" "Maha_South _ 2" "Maha_South _ 3" "Maha_South _ 4"
newlist=gsub(" ","",newlist)
newlist
[1] "UP_North" "UP_West" "UP_East" "UP_South"
[5] "MP_North" "MP_West" "MP_East" "MP_South"
[9] "Raj_North" "Raj_West" "Raj_East" "Raj_South"
[13] "Maha_North" "Maha_West" "Maha_East" "Maha_South"
[17] "UP_North_1" "UP_North_2" "UP_North_3" "UP_North_4"
[21] "UP_West_1" "UP_West_2" "UP_West_3" "UP_West_4"
[25] "UP_East_1" "UP_East_2" "UP_East_3" "UP_East_4"
[29] "UP_South_1" "UP_South_2" "UP_South_3" "UP_South_4"
[33] "MP_North_1" "MP_North_2" "MP_North_3" "MP_North_4"
[37] "MP_West_1" "MP_West_2" "MP_West_3" "MP_West_4"
[41] "MP_East_1" "MP_East_2" "MP_East_3" "MP_East_4"
[45] "MP_South_1" "MP_South_2" "MP_South_3" "MP_South_4"
[49] "Raj_North_1" "Raj_North_2" "Raj_North_3" "Raj_North_4"
[53] "Raj_West_1" "Raj_West_2" "Raj_West_3" "Raj_West_4"
[57] "Raj_East_1" "Raj_East_2" "Raj_East_3" "Raj_East_4"
[61] "Raj_South_1" "Raj_South_2" "Raj_South_3" "Raj_South_4"
[65] "Maha_North_1" "Maha_North_2" "Maha_North_3" "Maha_North_4"
[69] "Maha_West_1" "Maha_West_2" "Maha_West_3" "Maha_West_4"
[73] "Maha_East_1" "Maha_East_2" "Maha_East_3" "Maha_East_4"
[77] "Maha_South_1" "Maha_South_2" "Maha_South_3" "Maha_South_4"
請添加示例數據和預期輸出而不是描述。 – mtoto
'unique()'數據集和'paste()'與'sep ='_''在一起的狀態,地區和村莊?添加了 – janosdivenyi
。獨特是獲得所有可能組合的好方法 –