0

I'm a beginner in R. In order to classify characters according to factors, I'm trying to match strings from variable X with factors of the variable Y.

Which function or code in R, to match or assign the character to factors which are listed in different variable of a data frame.

I have a data.frame with two variables

      a    b
1  maison  fab
2   villa  mar
3  studio  seb
4  maison  fab
5   villa  art
6  maison  sol
7  maison  mar
8  studio  fab
9  studio  fab
10  villa  stev

I would like to get the following result:

  a        b
1  villa     fab
2  studio    mar
3  maison    seb
4  villa     fab
5  maison    art 
6  studio    Sol
7  studio    mar
8  villa     fab
9  villa     fab
10 villa     stev 
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Feriel
  • 13
  • 4
  • I would suggest turning them both into character vectors using `as.character()`. You can then use `match()` to align them. (I am assuming the strings in X are the same as those in Y other than the factor classification) – Bishops_Guest Jan 10 '18 at 20:22
  • 4
    It's easier to help you if you provide a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output. It's unclear exactly what you are asking and an example would help clear that up. – MrFlick Jan 10 '18 at 20:22
  • No the strings are not the same, in one of the variable, it's fuction title as Teacher the second is a short describtion of the job title as "mathematical teacher" there is different descriptions in most of the time they are not well matched, some mathematical teacher is alligner with Financial – Feriel Jan 10 '18 at 20:47
  • OK, you have given sample input and sample output, but you have given no explanation of how to go from one to the other. For example, Your output has "fab" next to most of the instances of "villa". Why? What connects "fab" to "villa"? – G5W Jan 10 '18 at 21:01
  • sorry for the explannation villa maison and studio are factors , we are classifing everyone one is named feb has a villa. – Feriel Jan 10 '18 at 21:16
  • Suggested duplicate: [Create a new variable from a lookup table](https://stackoverflow.com/q/8433523/903061). (Presumably you have the changes you want to make ("everyone is named feb has a villa") somewhere other than in your head, somewhere like a lookup table). – Gregor Thomas Jan 10 '18 at 21:30

0 Answers0