mercredi 15 mai 2019

Paste string if condition matches

I have a small question regarding to paste function in r. Here is my Table:

Name        Value
AA          0       
BB          1       
CC          0   
DD          1       
EE          1       
FF          1       
GG          0   
HH          1

What I want to do is to get another column nameed Category in Table, depend on Name column in such a way that if AA or BB in column then in Category column must paste Duke, for CC category should be Mike, for DD or EE category should be Mark and so on. In the End I should get a Table

Name      Value        Category
AA          0           Duke    
BB          1           Duke    
CC          0           Mike
DD          1           Mark    
EE          1           Mark    
FF          1           Tom 
GG          0           Hex
HH          1           Tom

Table is quite big and impossible to make it manually so is there any smart way to read Name column and paste in Category column? Any loop function or combination of loop and paste function in r ? I search on stackoverflow but couldn't find a similar case. What I was thinking is something like this: Table$Category <- within(Table,ifelse(Name=="AA" | Name== "BB",paste("Duke")), ifelse (Name=="CC" ,paste("Mike")), ifesle(Name=="DD" | Name== "EE",paste("Mark")) Can someone have a look over this? Thank you.

Aucun commentaire:

Enregistrer un commentaire