13 Tipul de data factor

x <- c('m','f','f','f','m','n')
x
## [1] "m" "f" "f" "f" "m" "n"
x_factor <- factor(x)
x
## [1] "m" "f" "f" "f" "m" "n"
x_factor
## [1] m f f f m n
## Levels: f m n
plot(x_factor)
## [1] FALSE
is.factor(x_factor)
## [1] TRUE
x_factor_nivele <- factor(x, levels=c('m','f','n','t'))
x_factor_nivele
## [1] m f f f m n
## Levels: m f n t
levels(x_factor)
## [1] "f" "m" "n"
levels(x_factor_nivele)
## [1] "m" "f" "n" "t"
x_factor
## [1] m f f f m n
## Levels: f m n
x_factor_nivele
## [1] m f f f m n
## Levels: m f n t
plot(x_factor)
plot(x_factor_nivele)
inaltime=c(143,156,163,147)
greutate=c(45,51,56,46)
gen=c('m','n','m','f')
tabel <- data.frame(inaltime, greutate, gen)
tabel$gen_f <- factor(tabel$gen)
x <- c(10,20,20,10,20,10,10,10)
x
## [1] 10 20 20 10 20 10 10 10
plot(x)
x_factor <- factor(x, labels=c('zece','douazeci'))
x_factor
## [1] zece     douazeci douazeci zece     douazeci zece     zece     zece    
## Levels: zece douazeci
x
## [1] 10 20 20 10 20 10 10 10
plot(x_factor)
x_factor <- factor(x, labels=c('douazeci', 'zece'))
x_factor
## [1] douazeci zece     zece     douazeci zece     douazeci douazeci douazeci
## Levels: douazeci zece
x
## [1] 10 20 20 10 20 10 10 10
x_factor <- factor(x, labels=c('zece','douazeci'))
x_factor
## [1] zece     douazeci douazeci zece     douazeci zece     zece     zece    
## Levels: zece douazeci
x
## [1] 10 20 20 10 20 10 10 10
plot(x_factor)
x_factor_ordonat <- ordered(x_factor, levels=c('douazeci','zece'))
plot(x_factor_ordonat)
ggplot(weather, aes(x=month, y=temp))+
  geom_boxplot()
## Warning: Removed 1 rows containing non-finite values (`stat_boxplot()`).
weather$luna <- factor(weather$month, labels=c('ian','feb','mar','apr','mai','iun','iul','aug','sep','oct','nov','dec'))
ggplot(weather, aes(x=luna, y=temp))+
  geom_boxplot()
## Warning: Removed 1 rows containing non-finite values (`stat_boxplot()`).