PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, May 17, 2022

[FIXED] How to merge two data frames based on part of a character variable?

 May 17, 2022     merge, partial, r     No comments   

Issue

I need to merge two datasets. The first dataset is the original one that I am working with and the second has data that I need to add. I would merge them based on the company name because that character variable is in both datasets, but they're presented differently (e.g. "Apple Inc." in one data frame and "Apple" in the other) and so I can't just use merge() like I otherwise would.

I think the best way is to try merge them based on these variables having the first x number of letters in common, but I don't know how to do this, nor do I know if this is even the best way to go about this.

Can anyone please help me with this? I have only been using R for a few months and don't have a programming background so this stuff doesn't come naturally to me.


Solution

A simple workaround would be to add a column with only the substring and use it for merging:

x$merge.col <- substr(x$company.name, 1, 5)
y$merge.col <- substr(y$company.name, 1, 5)
z <- merge(x, y, by="merge.col")


Answered By - cdalitz
Answer Checked By - Candace Johnson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing