I have a data.frame in R:

```
user    hobby      user_profile_url
1       reading    "https://...user1"
1       dancing    "https://...user1"
2       dancing    "https://...user2"
2       gaming     "https://...user2"
3       gaming     "https://...user3"
4       cooking    "https://...user4"
4       singing    "https://...user4"
...

```

I'm trying to choose a unique user for each unique hobby (as long as it possible).

Desired output:

```
user    hobby      user_profile_url
1       reading    "https://...user1"
2       dancing    "https://...user2"
3       gaming     "https://...user3"
4       cooking    "https://...user4"
4       singing    "https://...user4"
...

```

Does anyone have any idea how to do this? Thanks!

**Edit:**

I think I have something that works. 

```
uniqueUserPerHobby <- function(df){
  vec1Arr <- c()
  vec2Arr <- c()
  used_id <- c()
  for (a_label in unique(df$hobby)){
    if (nrow(df[df$hobby==a_label,])==1) {
      used_id <- c(used_id, df[df$hobby==a_label,]$user )
      vec1Arr <- c(vec1Arr, df[df$hobby==a_label,]$user)
      vec2Arr <- c(vec2Arr, a_label)
    } else {
      i<-1
      df_multy <- df[df$hobby==a_label,]
      for (a_user in df_multy$user) {
        i<-i+1
        if (nrow(df[df$user==a_user,])==1) {
          used_id <- c(used_id, a_user)
          vec1Arr <- c(vec1Arr, a_user)
          vec2Arr <- c(vec2Arr, a_label)
          break
        } else if (i == length(df$user)) {
          if (! a_user %in% used_id){
            used_id <- c(used_id, a_user)
          }
          vec1Arr <- c(vec1Arr, a_user)
          vec2Arr <- c(vec2Arr, a_label)
        } else if (! a_user %in% used_id) {
          used_id <- c(used_id, a_user)
          vec1Arr <- c(vec1Arr, a_user)
          vec2Arr <- c(vec2Arr, a_label)
          break
        }
      }
      if (!a_label %in% vec2Arr){
        if (!df[df$hobby==a_label,]$user[1] %in% used_id){
          used_id <- c(used_id, df[df$hobby==a_label,]$user[1])
        }
        vec1Arr <- c(vec1Arr, df[df$hobby==a_label,]$user[1])
        vec2Arr <- c(vec2Arr, a_label)
      }
    }
  }
  new.df <- dplyr::left_join(data.frame(user=vec1Arr, hobby=vec2Arr, stringsAsFactors = F), df, by=c("user", "hobby"))
  return(new.df)
}

```

This is probably not the best way to do this, but I tried it on two different data.frames and it worked.

```
> dat <- data.frame(user  = c(1,1,2,2,3,4,4), hobby = c("reading","dancing","dancing","gaming","gaming","cooking","singing"), user_profile_url = c("https://...user1","https://...user1","https://...user2","https://...user2","https://...user3","https://...user4","https://...user4"), stringsAsFactors = F)

> uniqueUserPerHobby(dat)

  user   hobby user_profile_url
1    1 reading https://...user1
2    2 dancing https://...user2
3    3  gaming https://...user3
4    4 cooking https://...user4
5    4 singing https://...user4

```