0

I would like to replace the 1-values in a dataset column by a character. I tried with

dataset$out[dataset$out==1]<-'A'

but I obtain the warning message:

Warning message: In `[<-.factor`(`*tmp*`, dataset$out == 1, value = c(1L, 1L,  :
invalid factor level, NA generated

What I did wrong?

Mark
  • 1,577
  • 16
  • 43
  • I think a similar problem solution can be found [Warning message: In `…` : invalid factor level, NA generated](https://stackoverflow.com/questions/16819956/warning-message-in-invalid-factor-level-na-generated) – deepseefan Nov 27 '19 at 15:39
  • 3
    Cast `dataset$out` from `factor` to `character` with `dataset$out <- as.character(dataset$out)` before you assign `A` to it. Or bring `A` to the levels of `dataset$out`. – GKi Nov 27 '19 at 15:44

1 Answers1

1

as the comments above have pointed out, your column "out" is a factor, try str(dataset$out). In factors, you have levels which are predefined values, and they are used for many purposes.

For example:

x = LETTERS[1:5]
x = x[-1]
table(x[-1])

x = factor(LETTERS[1:5])
levels(x)
x = x[-1]
levels(x)
table(x[-1])

In the example above, even if you remove 'A' from the vector x, because the levels are predefined, it shows you it is missing 'A' when you table it.

So you cannot replace an element in a factor column, with something that doesn't exist in the levels.

x = factor(LETTERS[1:5])
# ok
x[1] = "E"
# not ok
x[1] = "F"

So for your data, do:

dataset <- data.frame(id=1:5,out=c('1',LETTERS[2:5]))
dataset

  id out
1  1   1
2  2   B
3  3   C
4  4   D
5  5   E

dataset$out <- with(dataset,replace(as.character(out),out=='1','A'))
dataset

  id out
1  1   A
2  2   B
3  3   C
4  4   D
5  5   E

dataset$out <- factor(dataset$out)

In the above, I am converting them to a character first, then replacing those that are '1' with 'A'. You can convert them back to factor afterwards, if need be.

StupidWolf
  • 45,075
  • 17
  • 40
  • 72