Replacing works fine when I replace number with number or number with text.
> dat13 <- data.frame (A1 = 1:5, B1 = 11:15, C1 = 21:25)
> dat13[dat13 == 1] <- 999
> dat13
   A1 B1 C1
1 999 11 21
2   2 12 22
3   3 13 23
4   4 14 24
5   5 15 25
Replace number with text:
> dat13[dat13 == 999] <- "AAAA"
> dat13
    A1 B1 C1
1 AAAA 11 21
2    2 12 22
3    3 13 23
4    4 14 24
5    5 15 25
When I replace text with text, it does not work as is supposed to:
> dat12 <- data.frame (A1 = c("AAAA", "AAAB", "AABB", "ABBB", "BBBB"),B1 = c("AAAA", "AAAB", "AABB", "ABBB", "BBBB"), C1 = c("AAAA", "AAAB", "AABB", "ABBB", "BBBB"))
> dat12
    A1   B1   C1
1 AAAA AAAA AAAA
2 AAAB AAAB AAAB
3 AABB AABB AABB
4 ABBB ABBB ABBB
5 BBBB BBBB BBBB
> dat12[dat12 == "AAAA"] <- "AA"
Warning messages:
1: In `[<-.factor`(`*tmp*`, thisvar, value = "AA") :
  invalid factor level, NA generated
2: In `[<-.factor`(`*tmp*`, thisvar, value = "AA") :
  invalid factor level, NA generated
3: In `[<-.factor`(`*tmp*`, thisvar, value = "AA") :
  invalid factor level, NA generated
And text with number:
> dat12[dat12 == "AAAA"] <- 3
> dat12
    A1   B1   C1
1 <NA> <NA> <NA>
2 AAAB AAAB AAAB
3 AABB AABB AABB
4 ABBB ABBB ABBB
5 BBBB BBBB BBBB
Can you help me to solve the problem (with explanation) ?
 
     
     
    