如何用查找表中的值标签替换数字代码? [英] how do I replace numeric codes with value labels from a lookup table?
问题描述
此问题与此问题相关,但与不完全一样。
说我有这个数据框,
df< - data.frame(
id = c(1:6),
profession = c(1,5,4,NA,0,5))
以及一个有关行业代码的可读取信息的字符串。说,
profession.code< - c(
验光师= 1,会计师= 2,兽医= 3,
`财务分析师= 4,护士= 5)
现在,我在看为了将 df $ profession
中的值替换为 profession.code
中的文本的最简单方法。最好不使用特殊的图书馆,除非它明显缩短了代码。
我希望我的最终结果是
df< - data.frame(
id = c(1:6),
profession = c(Optometrists,Nurses,
财务分析师,NA,0,护士))
任何帮助将会非常大谢谢,
谢谢,
Eric
你可以这样做:
df< - data.frame(id = c(1:6),
职业= c(1,5,4,NA,0,5))
profession.code< - c(`0` = 0,验光师= 1,会计师= 2,兽医= 3,
`财务分析师= 4,护士= 5)
df $ profession.str < - names(profession.code)[match(df $ profession,profession.code) ]
df
#id行业专业.str
#1 1 1验光师
#2 2 5护士
#3 3 4财务分析师
#4 4 NA< NA>
#5 5 0 0
#6 6 5护士
请注意,不得不在您的专业代码
向量中添加一个 0
条目,以表示这些零。
编辑:这是一个更新的解决方案,以说明下面的Eric的评论,数据可能包含任何数量的专业代码,没有相应的描述:
match.idx< - match(df $ professional,profession.code)
df $ profession.str< - ifelse(is.na(match。 idx),
df $ professional,
names(profession.code)[match.idx])
This question is related to this question, but not quite the same.
Say I have this data frame,
df <- data.frame(
id = c(1:6),
profession = c(1, 5, 4, NA, 0, 5))
and a string with human readable information about the profession codes. Say,
profession.code <- c(
Optometrists=1, Accountants=2, Veterinarians=3,
`Financial analysts`=4, Nurses=5)
Now, I'm looking for the easiest way to replace the values in df$profession
with the text found in profession.code
. Preferably without use of special libraries, unless it shortens the code significantly.
I would like my end result to be
df <- data.frame(
id = c(1:6),
profession = c("Optometrists", "Nurses",
"Financial analysts", NA, 0, "Nurses"))
Any help would be greatly appreciated.
Thanks, Eric
You can do it this way:
df <- data.frame(id = c(1:6),
profession = c(1, 5, 4, NA, 0, 5))
profession.code <- c(`0` = 0, Optometrists=1, Accountants=2, Veterinarians=3,
`Financial analysts`=4, Nurses=5)
df$profession.str <- names(profession.code)[match(df$profession, profession.code)]
df
# id profession profession.str
# 1 1 1 Optometrists
# 2 2 5 Nurses
# 3 3 4 Financial analysts
# 4 4 NA <NA>
# 5 5 0 0
# 6 6 5 Nurses
Note that I had to add a 0
entry in your profession.code
vector to account for those zeroes.
EDIT: here is an updated solution to account for Eric's comment below that the data may contain any number of profession codes for which there are no corresponding descriptions:
match.idx <- match(df$profession, profession.code)
df$profession.str <- ifelse(is.na(match.idx),
df$profession,
names(profession.code)[match.idx])
这篇关于如何用查找表中的值标签替换数字代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!