简体   繁体   中英

How can I assign different values to each character taken from a character vector in R

Okay, so I have this character vector of row names.

> row.names(champs)
      [1] " Aatrox"       " Ahri"         " Akali"        " Alistar"     
      [5] " Amumu"        " Anivia"       " Annie"        " Ashe"        
      [9] " Blitzcrank"   " Brand"        " Braum"        " Caitlyn"     
     [13] " Cassiopeia"   " Cho'Gath"     " Corki"        " Darius"      
     [17] " Diana"        " Dr. Mundo"    " Draven"       " Elise"       
     [21] " Evelynn"      " Ezreal"       " Fiddlesticks" " Fiora"       
     [25] " Fizz"         " Galio"        " Gangplank"    " Garen"       
     [29] " Gragas"       " Graves"       " Hecarim"      " Heimerdinger"
     [33] " Irelia"       " Janna"        " Jarvan IV"    " Jax"         
     [37] " Jayce"        " Jinx"         " Karma"        " Karthus"     
     [41] " Kassadin"     " Katarina"     " Kayle"        " Kennen"      
     [45] " Kha'Zix"      " Kog'Maw"      " LeBlanc"      " Lee Sin"     
     [49] " Leona"        " Lissandra"    " Lucian"       " Lulu"        
     [53] " Lux"          " Malphite"     " Malzahar"     " Maokai"      
     [57] " Master Yi"    " Miss Fortune" " Mordekaiser"  " Morgana"     
     [61] " Nami"         " Nasus"        " Nautilus"     " Nidalee"     
     [65] " Nocturne"     " Nunu"         " Olaf"         " Orianna"     
     [69] " Pantheon"     " Poppy"        " Quinn"        " Rammus"      
     [73] " Renekton"     " Rengar"       " Riven"        " Rumble"      
     [77] " Ryze"         " Sejuani"      " Shaco"        " Shen"        
     [81] " Shyvana"      " Singed"       " Sion"         " Sivir"       
     [85] " Skarner"      " Sona"         " Soraka"       " Swain"       
     [89] " Syndra"       " Talon"        " Taric"        " Teemo"       
     [93] " Thresh"       " Tristana"     " Trundle"      " Tryndamere"  
     [97] " Twisted Fate" " Twitch"       " Udyr"         " Urgot"       
    [101] " Varus"        " Vayne"        " Veigar"       " Vel'Koz"     
    [105] " Vi"           " Viktor"       " Vladimir"     " Volibear"    
    [109] " Warwick"      " Wukong"       " Xerath"       " Xin Zhao"    
    [113] " Yasuo"        " Yorick"       " Zac"          " Zed"         
    [117] " Ziggs"        " Zilean"       " Zyra"

I want to assign an increasing number to every single name, example:

> Aatrox<-1
> Ahri<-2
> Akali<-3
> Aatrox
[1] 1
> Ahri
[1] 2
> Akali
[1] 3

But, I don't want to have to type out each name, so is there something like this that can get me the result above:

a<-length(row.names(champs))
for (i in 1:a){row.names(champs)[i]<-i}

Something like this? I just applied a sequence to the vector of names. This example is for the first 16.

> champs <- c(" Aatrox", " Ahri", " Akali", " Alistar", " Amumu", " Anivia",  
              " Annie", " Ashe", " Blitzcrank", " Brand", " Braum", " Caitlyn",  
              " Cassiopeia", " Cho'Gath", " Corki", " Darius")  
> x <- seq_along(champs)
> names(x) <- champs
> x
#     Aatrox        Ahri       Akali     Alistar       Amumu      Anivia 
#          1           2           3           4           5           6 
#      Annie        Ashe  Blitzcrank       Brand       Braum     Caitlyn 
#          7           8           9          10          11          12 
# Cassiopeia    Cho'Gath       Corki      Darius 
#         13          14          15          16 

Try:

 set.seed(42)
 champs <- data.frame(HP=sample(250:350, 10,replace=TRUE), HP1=rnorm(10))

 row.names(champs)<- c("Aatrox","Ahri","Akali","Alistar","Amumu","Anivia","Annie","Ashe",
 "Blitzcrank","Brand")


list2env(split(champs, row.names(champs)),envir=.GlobalEnv)
 #<environment: R_GlobalEnv>
 Brand
 #       HP        HP1
 #Brand 321 -0.1333213

Update

Using the same dataset

 list2env(split(1:nrow(champs), row.names(champs)),envir=.GlobalEnv)
 #<environment: R_GlobalEnv>

 Aatrox
 #[1] 1
 Ahri
 #[1] 2

Would this approach help? Obviously, match the sequence length to your champs length

champs<- c("Aatrox","Ahri","Akali","Alistar","Amumu","Anivia","Annie","Ashe",
+                       "Blitzcrank","Brand")

paste(seq(1:10), champs, sep = " ")
 [1] "1 Aatrox"     "2 Ahri"       "3 Akali"      "4 Alistar"    "5 Amumu"      "6 Anivia"     "7 Annie"     
 [8] "8 Ashe"       "9 Blitzcrank" "10 Brand"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM