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

Monday, August 29, 2022

[FIXED] How to split a dataframe into 2 by duplicated condition in R

 August 29, 2022     csv, dataframe, duplicates, r, split     No comments   

Issue

If I have a dataframe named df like so..

 ____________________
| id   |  name | age |
|____________________|
| 0123 | Joe   | 20  |            
|____________________|
| 0123 | Kyle  | 45  |              
|____________________|
| 0333 | Susan | 24  |            
|____________________|
| 0333 | Molly | 80  |              
|____________________|

How can I split this df into two so that neither df has any duplicate id values. Hence, I am looking for them to be like so...

 ____________________
| id   |  name | age |
|____________________|
| 0123 | Joe   | 20  |            
|____________________|
| 0333 | Susan | 24  |              
|____________________|

 ____________________
| id   |  name | age |
|____________________|
| 0333 | Molly | 80  |            
|____________________|
| 0123 | Kyle  | 45  |              
|____________________|

Let me know if you can help!


Solution

Here is a dplyr solution:

df1 <- df %>% 
  distinct(id, .keep_all = TRUE)

df2 <- anti_join(df, df1)
> df1
   id  name age
1 123   Joe  20
2 333 Susan  24
> df2
   id  name age
1 123  Kyle  45
2 333 Molly  80


Answered By - TarJae
Answer Checked By - Robin (PHPFixing Admin)
  • 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