好的,所以我有这个行名称的字符向量。
> 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"
我想为每个名称分配一个递增的数字,例如:
> Aatrox<-1
> Ahri<-2
> Akali<-3
> Aatrox
[1] 1
> Ahri
[1] 2
> Akali
[1] 3
但是,我不想输入每个名字,所以有这样的东西可以得到我上面的结果:
a<-length(row.names(champs))
for (i in 1:a){row.names(champs)[i]<-i}
答案 0 :(得分:1)
这样的东西?我只是将一个序列应用于名称向量。这个例子适用于前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
答案 1 :(得分:0)
尝试:
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
使用相同的数据集
list2env(split(1:nrow(champs), row.names(champs)),envir=.GlobalEnv)
#<environment: R_GlobalEnv>
Aatrox
#[1] 1
Ahri
#[1] 2
答案 2 :(得分:0)
这种方法有帮助吗?显然,将序列长度与您的冠军长度相匹配
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"