Reputation: 83
I wanna convert a column with three different character values to numeric.
the column is long but the head is: Yes No No Maybe No Yes
I want it to look like this: 2 1 1 3 1 2
the problem is that I wanna specify which value would get which number, its important that "Yes" would get the value 2, "No" would get the value 1 and "Maybe" get the value 3.
I tried using as.numeric but it decides what values on its own.
Upvotes: 0
Views: 532
Reputation: 198
You can also do this in a dplyrish way. In this case c is a dataframe
c = as.data.frame(c('Yes','No','No','Maybe','No','Yes'))
dplyr::case_when(
c == "Yes" ~ 2,
c == "No" ~ 1,
c == "Maybe" ~ 3
)
Upvotes: 0
Reputation: 601
Try this:
col=c("Yes", "No", "No", "Maybe", "No", "Yes")
col<-as.integer(factor(col,levels=c("No","Yes","Maybe")))
When you put the levels yo can order them whatever you want
Upvotes: 1