R4. 重命名一個(gè)數(shù)據(jù)集中的因子水平Renaming levels of a factor

Problem

You want to rename the levels in a factor.

Solution

# A sample factor to work with.
x<-factor(c("alpha","beta","gamma","alpha","beta"))
x
#> [1] alpha beta gamma alpha beta 
#> Levels: alpha beta gammalevels(x)
#> [1] "alpha""beta""gamma"

The easiest way is to use revalue() or mapvalues() from the plyr package:

library(plyr)
revalue(x, c("beta"="two", "gamma"="three"))
#> [1] alpha two three alpha two 
#> Levels: alpha two threemapvalues(x, from = c("beta", "gamma"), to = c("two", "three"))
#> [1] alpha two three alpha two 
#> Levels: alpha two three

If you don’t want to rely on plyr, you can do the following with R’s built-in functions.Note that these methods will modify x
directly; that is, you don’t have to save the result back into x.

# Renameby name: change"beta"to"two"
levels(x)[levels(x)=="beta"]<-"two"
# You can also renamebyposition, but this is a bit dangerous if your data
# can changein the future. If there is a changein the numberor positions of
# factor levels, then this can result in wrong data.
# Renamebyindexin levels list: change third item, "gamma", to"three".
levels(x)[3]<-"three"x
#> [1] alpha two three alpha two 
#> Levels: alpha two three
# Renameall levelslevels(x)<-c("one","two","three")
x
#> [1] one two three one two 
#> Levels: one two three

It’s possible to rename factor levels by name (without plyr), but keep in mind that this works only if ALL levels are present in the list; if any are not in the list, they will be replaced with NA.

# Rename all levels, by name
x<-factor(c("alpha","beta","gamma","alpha","beta"))
levels(x)<-list(A="alpha",B="beta",C="gamma")
x
#> [1] A B C A B#> Levels: A B C

It’s also possible to use R’s string search-and-replace functions to rename factor levels. Note that the ^ and $ surrounding alpha are there to ensure that the entire string matches. Without them, if there were a level named alphabet, it would also match, and the replacement would be onebet.

# A sample factor to work with.
x<-factor(c("alpha","beta","gamma","alpha","beta"))
x
#> [1] alpha beta gamma alpha beta 
#> Levels: alpha beta gamma
levels(x)<-sub("^alpha$","one",levels(x))
x
#> [1] one beta gamma one beta 
#> Levels: one beta gamma
# Across all columns, replace all instances of "a" with "X"
levels(x)<-gsub("a","X",levels(x))
x
#> [1] one betX gXmmX one betX 
#> Levels: one betX gXmmX
# gsub() replaces all instances of the pattern in each factor level.
# sub() replaces only the first instance in each factor level.

See also

Mapping values in a vector to new values works much the same. See
../Mapping vector values
../Mapping vector values
for more information.
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容