Reputation: 1023
I have a column with a series of ID's that I need to recode into something that's a bit more intuitive. This is an example of my file:
ID = c("DR-0001", "DR-0002", "DR-0003", "DR-0004", "DR-0001", "DR-0002", "DR-0001")
df <- data.frame(ID)
where I want to create a new column where I relabel entries. In pseudo code I want the following:
df$ID_useful = 2019/01 if df$ID == "DR-0001", 2015/06 if df$ID == "DR-0002" etc.
so that the end result would be something like this:
ID = c("DR-0001", "DR-0002", "DR-0003", "DR-0004", "DR-0001", "DR-0002", "DR-0001")
ID_useful = c("2019/01", "2015/06", "1995/02", "2012/08", "2019/01", "2015/06", "2019/01")
And I'm not sure how to do this without creating a billion ifelse command lines. Any advice is appreciated!
Upvotes: 3
Views: 70
Reputation: 101638
Another option with base R using setNames
dic <- sprintf("DR-%04d", 1:4)
dic_useful <- c("2019/01", "2015/06", "1995/02", "2012/08")
transform(
df,
ID_useful = setNames(dic_useful, dic)[ID]
)
gives
ID ID_useful
1 DR-0001 2019/01
2 DR-0002 2015/06
3 DR-0003 1995/02
4 DR-0004 2012/08
5 DR-0001 2019/01
6 DR-0002 2015/06
7 DR-0001 2019/01
Upvotes: 1
Reputation: 887183
If we make those unique key/value dataset, then it is more efficient with a join
library(dplyr)
keydat <- data.frame(ID = sprintf('DR-%04d', 1:4),
ID_useful = c("2019/01", "2015/06", "1995/02", "2012/08"))
df %>%
left_join(keydat)
-output
joining, by = "ID"
ID ID_useful
1 DR-0001 2019/01
2 DR-0002 2015/06
3 DR-0003 1995/02
4 DR-0004 2012/08
5 DR-0001 2019/01
6 DR-0002 2015/06
7 DR-0001 2019/01
Upvotes: 2