Thefactor() function creates a factor and defines the available levels. By default the levels are taken from the ones in the vector***. Actually, you don't often need to usefactor(), because when reading data in from a file, R assumes by default that text should be converted to factors (seeStatistical Analysis: an Introduction using R/R/R/Data frames). You may need to useas.factor(). Internally, R stores the levels as numbers from 1 upwards, but it is not always obvious which number corresponds to which level, and it should not normally be necessary to know.
ordered=TRUE.state.name #this is *NOT* a factorstate.name[1] <- "Any text" #you can replace text in a character vectorstate.region[1] <- "Any text" #but you can't in a factorstate.region[1] <- "South" #this is OKstate.abb #this is not a factor, just a character vector
character.vector <- c("Female", "Female", "Male", "Male", "Male", "Female", "Female", "Male", "Male", "Male", "Male", "Male", "Female", "Female" , "Male", "Female", "Female", "Male", "Male", "Male", "Male", "Female", "Female", "Female", "Female", "Male", "Male", "Male", "Female" , "Male", "Female", "Male", "Male", "Male", "Male", "Male", "Female", "Male", "Male", "Male", "Male", "Female", "Female", "Female") #a bit tedious to do all that typingCoded <- factor(c(1, 1, 2, 2, 2, 1, 1, 2, 2, 2, 2, 2, 1, 1, 2, 1, 1, 2, 2, 2, 2, 1, 1, 1, 1, 2, 2, 2, 1, 2, 1, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 1, 1, 1))Gender <- factor(Coded, labels=c("Female", "Male")) #we can then convert this to named levels