I don't understand why I got this warning message.
> fixed <- data.frame("Type" = character(3), "Amount" = numeric(3))
> fixed[1, ] <- c("lunch", 100)
Warning message:
In `[<-.factor`(`*tmp*`, iseq, value = "lunch") :
invalid factor level, NA generated
> fixed
Type Amount
1 <NA> 100
2 0
3 0
If you are reading directly from CSV file then do like this.
The easiest way to fix this is to add a new factor to your column. Use the levels function to determine how many factors you have and then add a new factor.
The warning message is because your "Type" variable was made a factor and "lunch" was not a defined level. Use the
stringsAsFactors = FALSE
flag when making your data frame to force "Type" to be a character.Here is a flexible approach, it can be used in all cases, in particular:
dataframe
has been obtained from applying previous operations (e.g. not immediately opening a file, or creating a new data frame).First, un-factorize a string using the
as.character
function, and, then, re-factorize with theas.factor
(or simplyfactor
) function: