mydata <- data.frame(id = 1:5,
item1 = c("", "", "1222", "1222", ""),
item2 = c("13", "", "", "", "382"))
> mydata
id item1 item2
1 1 13
2 2
3 3 1222
4 4 987
5 5 382
I have a dataset where it contains different codes. I want to map these codes to strings based on a dictionary
dictionary <- data.frame(code = c(1, 13, 382, 987, 1222),
entry = c("ballet", "soccer", "basketball", "painting", "pottery"))
> dictionary
code entry
1 1 ballet
2 13 soccer
3 382 basketball
4 987 painting
5 1222 pottery
The desired output is a data.frame with strings:
id item1 item2
1 1 soccer
2 2
3 3 pottery
4 4 pottery
5 5 basketball
4条答案
按热度按时间dohp0rv51#
Created on 2022-12-15 with reprex v2.0.2
NOTE:
Of course, a canonical way to do this is to use factors (and NA's instead of empty strings):
disbfnqx2#
May be a bit easier using a named vector as your dictionary. Using dplyr:
Or in base R:
Result from either approach:
wz1wpwve3#
This works pretty well.
2izufjch4#
Elaborating on the "canonical way" using
factor
s.The
|> lapply(droplevels)
cares for having just the levels that appear in each column. If you want character just replace it with|> lapply(as.character)