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

Tuesday, November 22, 2022

[FIXED] How to create a new variable based on the values in two columns

 November 22, 2022     dplyr, if-statement, multiple-conditions, r     No comments   

Issue

I want to add a new column to a dataframe based on the condition of two columns.

I have the following data:

Animal.1 <- c("A", "B", "C", "B", "A" )
Animal.2 <- c("B", "A", "A", "C", "C")
df <- data.frame(Animal.1, Animal.2)

If the following conditions are met:

Animal.1 = A and Animal.2 = B OR Animal.1 = B and Animal.2 = A

I would like the new column called pair.code to equal 1.

I would like a different number for every pair of animal ids, but the same number to be used if the same animal id's are found in either Animal.1 and Animal.2 OR Animal.2 and Animal.1.

The final data should look like this:

Animal.1 <- c("A", "B", "C", "B", "A" )
Animal.2 <- c("B", "A", "A", "C", "C")
pair.code <- c("1", "1", "2", "3", "2")


df <- data.frame(Animal.1, Animal.2)

Solution

We can first sort the elements by row and then create the 'pair.code' with match

m1 <- t(apply(df, 1, sort))
v1 <- paste(m1[,1], m1[,2])
df$pair.code <- match(v1, unique(v1))
df$pair.code
#[1] 1 1 2 3 2


Answered By - akrun
Answer Checked By - Marie Seifert (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