0

I have a data frame like this:

 id status
241 1
451 3
748 3
469 2
102 3
100 1
203 2

Now what I want to do is this:

1 corresponds to 'good' , 2 corresponds to 'moderate', 3 corresponds to 'bad'.

So my output should be like this:

 id status
241 good
451 bad
748 bad
469 moderate
102 bad
100 good
203 moderate

How to do this ? I tried to do this using if else but it is getting complicated.

1
  • 1
    level <- c("good", "moderate", "bad"); df$status <- level[df$status] should work. Commented Oct 17, 2015 at 9:37

2 Answers 2

2

It sounds like you want a labelled factor. You can try:

df$status <- factor(df$status, labels=c('Good','Moderate','Bad'))

> df
   id   status
1 241     Good
2 451      Bad
3 748      Bad
4 469 Moderate
5 102      Bad
6 100     Good
7 203 Moderate
Sign up to request clarification or add additional context in comments.

Comments

0

It depends on what is the type of status column. If they are not factors, you can do(as pointed out by Pascal)

level<-c("Good","Moderate","Bad")
df$status<-level[df$status]

data

df<-data.frame(item=c("apple","banana","orange","papaya","mango"),grade=c(1,3,2,1,3),stringsAsFactors=FALSE)

And if it is set as factors, you may(as pointed out by Jay)

df$status<-factor(df$status, labels=c('Good','Moderate','Bad'))

data

df<-data.frame(item=c("apple","banana","orange","papaya","mango"),grade=c(1,3,2,1,3),stringsAsFactors=TRUE)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.