0

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.

Sahara
  • 11
  • 1
  • 5
  • Just to remind that there are thousands of rows. Thanks. – Sahara Mar 21 '16 at 03:03
  • It's already a factor, which is stored as integers, so you can just use `as.integer(Faculty$Faculty)`. If you care what the numbers are, it's a bit more work. – alistaire Mar 21 '16 at 03:05
  • A look-up table is probably quicker if there are many rows, though 'thousands' is probably going to make no difference - see: http://stackoverflow.com/questions/18456968/how-do-i-map-a-vector-of-values-to-another-vector-with-my-own-custom-map-in-r?lq=1 – thelatemail Mar 21 '16 at 03:25
  • @alistaire yes we care bout numbers as codes are specified. – Sahara Mar 21 '16 at 03:41
  • ...then you need to show how the codes are stored and linked to the respective names. – alistaire Mar 21 '16 at 03:44
  • @alistaire codes are a separate list in doc format, they are not in the dataset. There are 26 faculty names in dataset which must be replaces with 26 codes. – Sahara Mar 21 '16 at 04:13
  • Well, have you imported them into R? Are they in a data.frame? A list? A named integer of character vector? Are you just going to type them all in? 26 levels is not that many; akrun's solution should work, if you substitute concise vectors for `levels` and `labels`. – alistaire Mar 21 '16 at 04:22

1 Answers1

2

We can use factor

  factor(Faculty$Faculty, levels= unique(Faculty$Faculty), 
         labels=c("23","34", "15","7","11"))
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Thanks but codes must be replaced with faculties throughout the dataset which is about 10 thousand rows, how do I do that? a friend suggested look-up table but I have not look into it. – Sahara Mar 21 '16 at 03:45
  • @Sahara If there are 1000s of levels, making a look up table could also takes some time. – akrun Mar 21 '16 at 04:02
  • 1
    Can you think of a more efficient solution? – Sahara Mar 21 '16 at 04:14
  • @Sahara It all depends on whether you have any pattern or not. If the new labels have no direct connection whatsoever with the previous one, making the lookup table is the option. – akrun Mar 21 '16 at 04:16