Sahara
Sahara

Reputation: 11

To Replace character values with numbers

In my data set there is a column named "Faculty" that includes name of faculties. I want to replace the names with corresponding codes (numbers). For example, the following code produces a small sample.

Faculty<-data.frame(Faculty=c("Forestry","Advanced Technology", "erontology","Design and Architecture","Veterinary Medicine"))

In R, I want to replace all names with codes throughout the data frame. lets say:

Faculty.code<-data.frame(Faculty=c("23","34", "15","7","11"))

Thank you.

Upvotes: 0

Views: 169

Answers (1)

akrun
akrun

Reputation: 887951

We can use factor

  factor(Faculty$Faculty, levels= unique(Faculty$Faculty), 
         labels=c("23","34", "15","7","11"))

Upvotes: 2

Related Questions