Javier
Javier

Reputation: 730

Compare values across 2 columns using DPLYR

I'm trying to compare each row between 2 columns, using dplyr and mutate.

Dataframe

df <- data.frame(ID = c("1234", "1234", "7491", "7319", "321", "321"), 
add = c("1234", "1234", "749s1", "73a19", "321", "321"))

Mutate, if column ID = column add, return 1 else 0

df %>% mutate(TEST = ifelse(df$ID == df$add, 1, 0))

However, the above code does not seem to work.

Update: Error due to factor levels

Upvotes: 0

Views: 2634

Answers (1)

amrrs
amrrs

Reputation: 6325

You've not shared the error and I assume it's because of factor levels. Here's the updated solution.



library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

df <- data.frame(ID = c("1234", "1234", "7491", "7319", "321", "321"), 
                 add = c("1234", "1234", "749s1", "73a19", "321", "321"))



df %>% mutate(TEST = ifelse(as.character(ID) == as.character(add),1,0))
#>     ID   add TEST
#> 1 1234  1234    1
#> 2 1234  1234    1
#> 3 7491 749s1    0
#> 4 7319 73a19    0
#> 5  321   321    1
#> 6  321   321    1

Created on 2019-03-06 by the reprex package (v0.2.1)

You can further simplify it using as.numeric:

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

df <- data.frame(ID = c("1234", "1234", "7491", "7319", "321", "321"), 
                 add = c("1234", "1234", "749s1", "73a19", "321", "321"),
                 stringsAsFactors = FALSE)



df %>% mutate(TEST = as.numeric(ID == add))
#>     ID   add TEST
#> 1 1234  1234    1
#> 2 1234  1234    1
#> 3 7491 749s1    0
#> 4 7319 73a19    0
#> 5  321   321    1
#> 6  321   321    1

Created on 2019-03-06 by the reprex package (v0.2.1)

Upvotes: 2

Related Questions