I am trying to clean my data and can't find a way to replace values that are not according to my conditions like in the following example:
df1<- data.frame(
A=c("a","b","c"),
Ch=c("c1","xyz","c2"),
val=paste0("x",1:3), stringsAsFactors = FALSE)
all values that are different than c1, c2 I want to change into "other".
I tried:
for( i in 1:length(df)
if (df[i,2]==c1 | c2){
stay same vaue?!?
} else df[i,2] <- "other"
which did not work.
Any suggestions?
For replicability:
If you are trying to do it across all columns:
Base R solution: