I am trying to create the variable 'active' in a df conditional of values from a string in R (act_users). If the name from the variables scr_name and rt_name in the df is within the string, I would like the variable to take the value 1, if not 0.
df <- data.frame("screen_name" = c("august", "berit", "christopher", "david", "erica", "frank"), "rt_name" = c("berit", "august", "david", "erica", "frank", "christopher"))
act_users <- c("david", "august", "berit")
I have tried the following if else statements, but none of them work
'%!in%' <- function(x,y)!('%in%'(x,y))#create a function
df$active <- ifelse((df$screen_name %in% act_users) & (df$rt_name %in% act_users), 1,
ifelse((df$screen_name %!in% act_users) & (df$rt_name %!in% act_users), 2))
#attempts only with screenname
df$active <- ifelse(df$screen_name %in% act_users, "1", ifelse(df$screen_name %!in% act_users, "0"))
df$active <- if(df$screen_name %in% act_users){
df$active == 1
} else {
df$active == 0}
My last solution would be to make the active user string as a df, merge the results and match the colomns inside the dataframe, but my data is quite big, so it would be nice with a more efficient solution?
Thanks in adcvance!
Aucun commentaire:
Enregistrer un commentaire