1

list:

number name age status 
1      ben  34  m
2      max  12  s
3           27  s
4           42  d

I have this list and I have tried to replace the empty parts in "name" coulm.

this is my code (all my tries):

list$name[3] <- "eric"
list[3,2] <- "eric"
list[3,2] <- 0
list[is.na(list)] <- 0

this is the error:

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

Warning message:
In `[<-.factor`(`*tmp*`, thisvar, value = 0) :
  invalid factor level, NA generated

do you know what's the problem?

1 Answers1

0

name is a factor column and you're trying to add more "levels" which have not been defined.

You can either coerce the column to a character column and edit as you tried or add the levels to the factor column.

list$name <- as.character(list$name)
list$name[3] <- "eric"

or

list$name <- factor(list$name, levels=c(levels(list$name), "eric"))
list$name[3] <- "eric"

See this answer for more details.

Community
  • 1
  • 1
ruaridhw
  • 2,305
  • 8
  • 22