0

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) ?

Charles
  • 50,943
  • 13
  • 104
  • 142
jon
  • 11,186
  • 19
  • 80
  • 132
  • Friendly hint: Searching the web with the warning or error message is often more efficient than taking the time to ask a question on SO for these simple problems. – Roland Apr 09 '14 at 16:03
  • possible duplicate of [Rename characters as condition of another column, R](http://stackoverflow.com/questions/22967423/rename-characters-as-condition-of-another-column-r) – Tyler Rinker Apr 09 '14 at 16:12
  • I found this link useful too: http://stackoverflow.com/questions/2851015/convert-data-frame-columns-from-factors-to-characters – jon Apr 09 '14 at 16:30

2 Answers2

1

You need to set stringsAsFactors=F in your data.frame so that it isn't making your A1, B1, C1 variables into factors (which it is complaining about when you try to replace the value with a level that isn't present). This will make A1, B1, C1 character variables which will replace as expected.

dat12 <- data.frame (A1 = c("AAAA", "AAAB", "AABB", "ABBB", "BBBB"),B1 = c("AAAA", "AAAB", "AABB", "ABBB", "BBBB"), C1 = c("AAAA", "AAAB", "AABB", "ABBB", "BBBB"),stringsAsFactors=F)
dat12
dat12[dat12 == "AAAA"] <- "AA"

see ?data.frame for more info

Steve Reno
  • 1,304
  • 3
  • 14
  • 21
1

One possibility is:

dat12 <- as.data.frame(replace(as.matrix(dat12), dat12 == "AAAA", "AA")

    A1   B1   C1
1   AA   AA   AA
2 AAAB AAAB AAAB
3 AABB AABB AABB
4 ABBB ABBB ABBB
5 BBBB BBBB BBBB
Sven Hohenstein
  • 80,497
  • 17
  • 145
  • 168