I would like to change the name of the entries in the columns of a dataframe when the values of one of the columns are 0. I tried this way, but get an error message. Suppose I would like to change the entries of columns 1,2,3 to "A" when the entries of column 3 are 0
df[ df[ , 3 ] == 0 , c(1,2,4) ] <- "A"
I get
Warning message:
In `[<-.factor`(`*tmp*`, iseq, value = c("A", "A", "A", :
invalid factor level, NA generated
Should work after following command:
df[,c(1,2,4)]=sapply(df[,c(1,2,4)], function(x) as.character(x))
Can be shortened to following as suggested by josilber in the comments:
df[,c(1,2,4)]=sapply(df[,c(1,2,4)], as.character)