jon
jon

Reputation: 11366

replacing text value with text value in data frame in R

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

Upvotes: 0

Views: 1702

Answers (2)

Sven Hohenstein
Sven Hohenstein

Reputation: 81693

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

Upvotes: 1

Steve Reno
Steve Reno

Reputation: 1384

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

Upvotes: 1

Related Questions