Читать книгу The Big R-Book - Philippe J. S. De Brouwer - Страница 99
Warning – Avoiding conversion to factors
ОглавлениеThe default behaviour of R is to convert strings to factors when a data.frame is created. Decades ago this was useful for performance reasons. Now, this is usually unwanted behaviour.a To avoid this put stringsAsFactors = FALSE
in the data.frame()
function.
d <- data.frame( Name = c(“Piotr”, “Pawel”,“Paula”,“Lisa”,“Laura”), Gender = c(“Male”, “Male”,“Female”, “Female”,“Female”), Score = c(78,88,92,89,84), Age = c(42,38,26,30,35), stringsAsFactors = FALSE ) d$Gender <- factor(d$Gender) # manually factorize gender str(d) ## ‘data.frame’: 5 obs. of 4 variables: ## $ Name : chr “Piotr” “Pawel” “Paula” “Lisa” … ## $ Gender: Factor w/ 2 levels “Female”,“Male”: 2 2 1 1 1 ## $ Score : num 78 88 92 89 84 ## $ Age : num 42 38 26 30 35