1

I have a dataframe with two columns: df$user and df$type. The users are a list of different user names and the type category has two values: 'high_user' and 'small_user'

I want to create some code so that one user cannot be both types. For example if the user is high_user he cannot also be a small_user.

head(df$user)

[1] RompnStomp       Vladiman         Celticdreamer54  Crimea is Russia shrek1978third   annietattooface 

head(df$type)

"high_user" "high_user" "small_user" "high_user" "high_user" "small_user"

Any help would be greatly appreciated.

tom
  • 725
  • 4
  • 17
Ana Wilmer
  • 63
  • 5
  • 1
    welcome to SO. please provide a minimal reproducible example with example data and the expected output: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – starja May 29 '20 at 09:06
  • How do you choose the type, if one user has two or more types? – s__ May 29 '20 at 09:24

2 Answers2

1

One way would be to assign the first value of User to all the values of it's type.

df$new_type <- df$type[match(df$User, unique(df$User))]
df

#  User       type   new_type
#1    a  high_user  high_user
#2    b  high_user  high_user
#3    a small_user  high_user
#4    c small_user small_user
#5    c  high_user small_user

This can also be done using grouped operations.

library(dplyr)
df %>% group_by(User) %>% mutate(new_type = first(type))

data

df <- data.frame(User = c('a', 'b', 'a', 'c', 'c'), 
      type = c('high_user', 'high_user', 'small_user', 'small_user', 'high_user'))
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
1

An option with base R

df$new_type <- with(df, ave(type, User, FUN = function(x) x[1]))

data

df <- data.frame(User = c('a', 'b', 'a', 'c', 'c'), 
      type = c('high_user', 'high_user', 'small_user', 'small_user', 'high_user'))
akrun
  • 874,273
  • 37
  • 540
  • 662