-1

I'm trying to replace a cell of a data frame, which I generated earlier in my code, with another text.

for (i in 1:length(flatfileFltrd)){
  rename = paste("WEN", "\\", flatfileFltrd[i,8], sep = "")
  flatfileFltrd$fileName[i] = rename
}

which returns:

In `[<-.factor`(`*tmp*`, i, value = "WEN\\UA") :
  invalid factor level, NA generated

I tried to figure out what type of a data frame I have as I'm assuming the problem goes back that as they're not both the same but no luck. is.atomic(flatfileFltrd$fileName[i]) returns TRUE

How do I fix this? is there a better way to replace my data elements?

AA16
  • 27
  • 2
  • A `factor` is an an enumeration with a fixed set of possible values. For instance, imagine if we defined an enumeration of the first 10 letters of the alphabet through "J". If at some point you want to arbitrarily add "K" to the list of enumerated letters, then you either (1) automatically add it to the list, which R does *not* do; (2) fail to recognize it and mark it as `NA` which effectively means *"I don't know, it could be any value"*. Sometimes option 1 could be the wrong thing to do, which is why (I believe) R forces the second path. Fix: convert to string or re-factor the enumerations. – r2evans Jan 24 '19 at 01:27

1 Answers1

1

This happens because factors have specific allowable levels. There are two ways to deal with this:

  1. Convert the factors to characters first:

flatfileFltrd$fileName <- as.character(flatfileFltrd$fileName)

  1. Add the new level to the existing factor levels:

levels(flatfileFltrd$fileName) <- c(levels(flatfileFltrd$fileName), rename)

morgan121
  • 2,213
  • 1
  • 15
  • 33