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

Saturday, October 29, 2022

[FIXED] How to join a dataset by row index?

 October 29, 2022     left-join, r     No comments   

Issue

I have one dataset that looks like this

Rownumber Name
5 John
7 Ken
9 Marvin

I have another that looks like this

LastName
Ming
Roe
Martin
Hugo
Sawyer
Wallace
Thomas
Wang
Abdul

Note that I want to join second dataset to first. In the first I have a column called row number and the second I don't, but I have a backend index.

I would like my dataset to look like this

Rownumber Name Last name
5 John Sawyer
7 Ken Thomas
9 Marvin Abdul

You see that in the second dataset Sawyer is the 5th row, Thomas is the 7th, and Abdul is the 9th that is way it merged.


Solution

Suppose your data are stored in two data.frames named df and df2, you could use a dplyr join:

library(dplyr)
df2 %>% 
  mutate(Rownumber = row_number()) %>% 
  right_join(df, by = "Rownumber") %>% 
  select(Rownumber, Name, LastName)

This returns

  Rownumber   Name LastName
1         5   John   Sawyer
2         7    Ken   Thomas
3         9 Marvin    Abdul
  • First create row numbers for your data.frame containing the last names.
  • Next right_join the first data.frame by Rownumber.

Data

df <- structure(list(Rownumber = c(5, 7, 9), Name = c("John", "Ken", 
"Marvin")), row.names = c(NA, -3L), class = "data.frame")

df2 <- structure(list(LastName = c("Ming", "Roe", "Martin", "Hugo", 
"Sawyer", "Wallace", "Thomas", "Wang", "Abdul")), row.names = c(NA, 
-9L), class = "data.frame")


Answered By - Martin Gal
Answer Checked By - Dawn Plyler (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