Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
Example of converting a factor into a character vector in Lesson 5 is a bit awkward #313
In the Factors section of Lesson 5, one solution to the error given when trying to add a row containing a level, tortoiseshell, to the factor cats$coat is given as adding the level to the factor first, i.e.
The other solution of converting cats$coat to a character vector makes less sense, since cats$coat is naturally a factor, hence this conversion is not a good solution to changing the data so that no error is obtained when adding a new row to the cats.
The intent appears to be to show how as.character() works as well as how to add a level to a category. One suggestion would that if a cat 'name' column was added to the cats data in Lessons 4 and 5, i.e. the feline-data.csv contained:
then the distinction between factor (cats$coat) and as.character (cats$name) data might be made more clearly, and there would be less confusion caused by making naturally categorical data into character data.
[p.s. Thanks for these great lessons, I've really enjoyed teaching them!]
Hi @doktor-nick thanks for logging the issue. I think you've landed on something that definitely needs fixing.
I think some might argue that it's best to keep things as character vectors for as long as possible (the whole
As I understand it the tidyverse approach to this issue is to not convert anything into a factor unless absolutely necessary. So it could be reasonable to turn the column into a character vector, but at the same time, I see your point about that allowing people to become careless with, e.g. typos.
I also just noticed as I was looking over it that the failed
str(cats) # 'data.frame': 5 obs. of 4 variables: # $ coat : Factor w/ 4 levels "black","calico",..: 2 1 3 NA 4 # $ weight : num 2.1 5 3.2 3.3 3.3 # $ likes_string: int 1 0 1 1 1 # $ age : num 4 5 8 9 9
Seems like that should be fixed too.
I think one way to solve this is to get rid of the as.character bit altogether - I'm a little hesitant for us to add more content (e.g. a bit on cat names) in a section that is already a little into the weeds for beginners.