通过连接三个字符串列表在R中创建唯一的ID

时间:2016-11-30 13:32:18

标签: r string

来自学生的问题

我们如何在R

中创建一个独特的I' d

例如,如果我们有数据集,其中有3个变量state,district和villages。

如果一个地区有3个村庄,并且所需的州没有6则需要创建独特的I' d是6 * 3 = 18

,我需要创建独特的I&#d; d

例如abc_1_1,abc_1_2,abc_1_3,abc_2_1,abc_2_2,abc_2_3 ..等等。

实施例

假设东德里是一个区,有3个村庄。因此,对于一个村庄,输出应该是东德里亚_1_1,东德里亚_1_2,东德里亚_1,等等,其他村庄的输出应该是18个,我应该创建。

3 个答案:

答案 0 :(得分:3)

我认为你可以解决这个问题。

sapply(states, function(x) paste0(x, districts))

  UP        MP        Raj        Maha       
[1,] "UPNorth" "MPNorth" "RajNorth" "MahaNorth"
[2,] "UPWest"  "MPWest"  "RajWest"  "MahaWest" 
[3,] "UPEast"  "MPEast"  "RajEast"  "MahaEast" 
[4,] "UPSouth" "MPSouth" "RajSouth" "MahaSouth"

答案 1 :(得分:1)

这是你在找什么?

states=c("UP","MP")
districts=c("North","South")
villages=c("1","2")
df <- data.frame(states = states, districts = districts, villages= villages)
df1 <- expand.grid(df)
#df1
#  states districts villages
#1     UP     North        1
#2     MP     North        1
#3     UP     South        1
#4     MP     South        1
#5     UP     North        2
#6     MP     North        2
#7     UP     South        2
#8     MP     South        2

library(tidyr)
unite(df1, new_col,states, districts, villages,sep="_")
#     new_col
#1 UP_North_1
#2 MP_North_1
#3 UP_South_1
#4 MP_South_1
#5 UP_North_2
#6 MP_North_2
#7 UP_South_2
#8 MP_South_2

答案 2 :(得分:0)

我的答案在这里,但如果有更优雅的方式我期待它

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"