Reputation: 103
Surely I am missing something here. I am reading this documentation:
Now, if I have any old vector:
x <- letters
I can do this:
x[5] <- "test"
... and the letter "e" in the vector x will be replaced by the string "test". So far so good, but if I make a data frame:
df <- data.frame(col1 = letters, col2 = letters)
then why do I get errors for:
df[5,1] <- "test"
and
df$col1[5] <- "test"
?
There MUST be an extremely simple way to do this, to get in to a data frame and change just one single value.
The error is "invalid factor level." So I suppose that ideally the way would be to somehow tell R that I want it to NOT treat the relevant column in the DF as a factor variable.
Upvotes: 0
Views: 92
Reputation: 42649
Use the stringsAsFactors
argument to data.frame
:
df <- data.frame(col1 = letters, col2 = letters, stringsAsFactors=FALSE)
This will give columns of mode character
, as you want.
?data.frame
shows its options.
Upvotes: 6