0

I have two Data frames. The first one is jobs:

Jobs <- data.frame(Company = c("A","B"), Name = c("Peter","Peter"), Job = c("CEO","Member of the Board"))

The Second one is Media_Appearence:

Media_Appearence <- data.frame(Company = c("A","A","B","B","A","A","A","A"),Name = c("Peter","Peter","Peter","Peter","Peter","Peter","Peter","Peter"))

Is there any way, using basic R commands,to insert a new column on Media_Appearence filling it using matching arguments (Checking First two columns anda returning the matching job) ? The desired output would be:

Media_Appearence <- data.frame(Company = c("A","A","B","B","A","A","A","A"),Name = c("Peter","Peter","Peter","Peter","Peter","Peter","Peter","Peter"),Job= c("CEO","CEO","Member of the Board","Member of the Board","CEO","CEO","CEO","CEO"))

Already tried to merge but results were messy.

Thanks

Hack-R
  • 22,422
  • 14
  • 75
  • 131
WsnCode
  • 39
  • 5
  • 2
    I noticed that you didn't accept any answer on any of the questions you asked. Although it is not mandatory to accept an answer, it is considered good practice to do so if one of the answers worked for you. This will give future readers a clue about the value of the solution. See also this help page: [What should I do when someone answers my question?](http://stackoverflow.com/help/someone-answers) – Jaap Oct 03 '17 at 16:42
  • I'll mark them, tks – WsnCode Oct 03 '17 at 17:58

2 Answers2

3

What you want to do is commonly called a "join", "merge" or "appending" data. R wouldn't be much of a language if you couldn't do that!

The field that is matched on is called a "key". In this case, R assumes your key is Company, because it is the only field they have in common.

Jobs <- data.frame(Company = c("A","B"), Name = c("Peter","Peter"), Job = c("CEO","Member of the Board"))

Media_Appearence <- data.frame(Company = c("A","A","B","B","A","A","A","A"),Name = c("Peter","Peter","Peter","Peter","Peter","Peter","Peter","Peter"))

merge(Media_Appearence, Jobs)
  Company  Name                 Job
1       A Peter                 CEO
2       A Peter                 CEO
3       A Peter                 CEO
4       A Peter                 CEO
5       A Peter                 CEO
6       A Peter                 CEO
7       B Peter Member of the Board
8       B Peter Member of the Board

You may have noticed that your data was sorted for you. It was sorted by the default key, which in this case is Company.

pyll
  • 1,688
  • 1
  • 26
  • 44
Hack-R
  • 22,422
  • 14
  • 75
  • 131
1

By using match , would not change the original order of data.frame

Media_Appearence$Job=Jobs$Job[match(Media_Appearence$Company, Jobs$Company)]
Media_Appearence
  Company  Name                 Job
1       A Peter                 CEO
2       A Peter                 CEO
3       B Peter Member of the Board
4       B Peter Member of the Board
5       A Peter                 CEO
6       A Peter                 CEO
7       A Peter                 CEO
8       A Peter                 CEO
BENY
  • 317,841
  • 20
  • 164
  • 234