check if the user is using the same IPs in R

Viewed 25

I have a data frame with payments data. I have the e-mail and the ip of the user who is making a transaction. I want to analyze the users who are trying to pay multiple times with the same e-mail but different IPs the two columns that I need from the main report are this:

| IP              | shopper_email       |
|-----------------|---------------------|
| 61.184.172.226  | test@gmail.com      |
| 61.184.172.226  | test@gmail.com      |
| 61.184.172.226  | test@gmail.com      |
| 204.171.147.183 | test@gmail.com      |
| 7.53.60.110     | shopper@shopper.com |
| 184.64.255.108  | hello@cloud.com     |

I would like to make a new column with a value that I could use as key to extrapolate the users that changed ips with something like this

| IP              | shopper_email       | change_ip |
|-----------------|---------------------|-----------|
| 61.184.172.226  | test@gmail.com      | yes       |
| 61.184.172.226  | test@gmail.com      | yes       |
| 61.184.172.226  | test@gmail.com      | yes       |
| 204.171.147.183 | test@gmail.com      | yes       |
| 7.53.60.110     | shopper@shopper.com | no        |
| 184.64.255.108  | hello@cloud.com     | no        |

I was thinking about a loop but maybe there's a simpler way to do this any suggestion?

like a combination with paste and match

dataf <- dataf %>% unite("change_ip", ip:shopper_mail)

1 Answers

If the intention to check whether each unique 'shopper_email' use more than one 'IP', use n_distinct to find the number of unique 'IP' grouped by 'shopper_email' and create a logical vector with > 1, convert the logical to 'yes', 'no'

library(dplyr)
df1 <- df1 %>%
       group_by(shopper_email) %>%
       mutate(change_ip = c('no', 'yes')[1 + (n_distinct(IP) > 1)]) %>%
       ungroup
Related