Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
324 views
in Technique[技术] by (71.8m points)

r - how do I replace numeric codes with value labels from a lookup table?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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])

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...