I have a DF. If the 'name1' column is not blank or not contains a specific value (e.g. christopher), I would like to paste some info. If the column contains a blank value or 'christopher' I would like these values to be maintained.
I have tried with different if-else sentences combined with OR, none of them lead me to the desired result. Because the names in the DF can differ, I am looking for a %like%-solution or similar and not an exact match (except from the blank values)
df <- data.frame("name1" = c("august", "", "christopher", "david", "erica", ""), "name2" = c("berit", "august", "david", "erica", "frank", "christopher"), stringsAsFactors = F)
#IF-ELSE sentence with '%!like%'
'%!like%' <- function(x,y)!('%like%'(x,y))
df$name1 <- ifelse(df$name1 !='' | df$name1 %!like% ('christopher') ,paste('Something to be pasted'), df$name1)
#Nested IF-ELSE sentence with '%!like%'
df$name1 <- ifelse(df$name1 =='', '',
ifelse(df$name1 %!like% ('christopher') | df$name1 !='' , paste('Something to be pasted'),
ifelse(df$name1 %!like% ('christopher'),df$name1))
My expected result is
df_exp <- data.frame("name1" = c("Something to be pasted", "", "christopher", "Something to be pasted", "Something to be pasted", ""), "name2" = c("berit", "august", "david", "erica", "frank", "christopher"))
Aucun commentaire:
Enregistrer un commentaire