これらの特殊文字をgsub
削除してから、recode
with(data3, recode(gsub("[/:]", "", Edu),
"'Incomplete secondary school technical vocational type' = 4"))
または、を新しいレベルにbase R
割り当てるなどの方法を使用できますlevels
levels(data3$Edu)[levels(data3$Edu)=="Incomplete secondary school: technical/ vocational type"] <- 4
data3
# Edu
#1 4
#2 Something else
#3 Some other thing
#4 4
アップデート
OP は複数の値を別のものに再コード化したいので、 を使用できますmatch
。ここでは、1 番目と 3 番目level
を新しい値に変更しています。
levels(data3$Edu)[match(levels(data3$Edu)[c(1,3)], levels(data3$Edu) ) ] <- c(4, 1)
data3
# Edu
#1 4
#2 1
#3 Some other thing
#4 4
OPがすべてのレベルをいくつかの数値に変更したい場合は、factor
レベルを数値に直接強制することができます
as.integer(data3$Edu)
#[1] 1 3 2 1
値は、levels
別の方法で設定することで変更できます。
as.integer(factor(data3$Edu, levels = levels(data3$Edu)[c(1,3,2)]))
#[1] 1 2 3 1
データ
data3 <- data.frame(Edu = c('Incomplete secondary school: technical/ vocational type',
'Something else',
'Some other thing',
'Incomplete secondary school: technical/ vocational type'
))