Showing posts with label R factors. Show all posts
Showing posts with label R factors. Show all posts

Friday, June 3, 2011

Creating Factors

reorder is fantastic to change the order of factors. If you import some data which contains a factor the factor levels are ordered according to the alphabet. This is also the sequence shown in graphics. So the levels "good", "better", "best" are displayed in reverse order.

Here are some examples showing how to create an ordered factor (in the order you want) out of an other:

if you have a numeric vector - and you want to label it
> taste <- rep(0:2,c(5,5,5)) # create the numeric vector > taste
[1] 0 0 0 0 0 1 1 1 1 1 2 2 2 2 2
# make a factor out auf it
> taste <- factor(taste, levels=0:2, labels=c("good","better","best"))

> taste
[1] good good good good good better better better better better
[11] best best best best best
Levels: good better best

> taste <- rep(0:2,5) # different use of rep

> taste
[1] 0 1 2 0 1 2 0 1 2 0 1 2 0 1 2

> taste <- factor(taste, levels=0:2, labels=c("good","better","best"))

> taste
[1] good better best good better best good better best good
[11] better best good better best
Levels: good better best

> taste <- letters[rep(1:3,5)] # create a vector containing characters/strings

> taste
[1] "a" "b" "c" "a" "b" "c" "a" "b" "c" "a" "b" "c" "a" "b" "c"

> taste <- factor(taste, levels=c("c","b","a"), labels=c("good","better","best"))

> taste
[1] best better good best better good best better good best
[11] better good best better good
Levels: good better best


there is also gl
> my.factor <- gl(number.of.levels,number.of.replications,labels=c(.....))
e.g.

> my.factor <- gl(3,4,labels=c("yes","maybe","no"))

> my.factor
[1] yes yes yes yes maybe maybe maybe maybe no no no no
Levels: yes maybe no

Wednesday, April 6, 2011

Changing the order of Factors (in plots)

use reorder

-> Here is the example from the internal help:

bymedian <- with(InsectSprays, reorder(spray, count, median))

boxplot(count ~ bymedian, data = InsectSprays,
xlab = "Type of spray", ylab = "Insect count",
main = "InsectSprays data", varwidth = TRUE,
col = "lightgray")


reorder(spray,count,median) reorders the factor levels of InsectSprays$spray (the first argument of reorder), not the factor itself;


the third argument of reorder - some aggregate function - operates on the column InsectSprays$count (the second argument of reorder) and defines therefore the new order.


In the example above the median of InsectSprays$count for each group of InsectSprays$spray is calculated, and the levels are reordered ascending according to the medians of each group.


look at the differences:

> InsectSprays$spray
[1] A A A A A A A A A A A A B B B B B B B B B B B B C C C C C C C C C C C C D D
[39] D D D D D D D D D D E E E E E E E E E E E E F F F F F F F F F F F F
Levels: A B C D E F
> with(InsectSprays, reorder(spray,count,median))
[1] A A A A A A A A A A A A B B B B B B B B B B B B C C C C C C C C C C C C D D
[39] D D D D D D D D D D E E E E E E E E E E E E F F F F F F F F F F F F
attr(,"scores")
A B C D E F
14.0 16.5 1.5 5.0 3.0 15.0
Levels: C E D A F B
> with(InsectSprays, reorder(spray,count,min))
[1] A A A A A A A A A A A A B B B B B B B B B B B B C C C C C C C C C C C C D D
[39] D D D D D D D D D D E E E E E E E E E E E E F F F F F F F F F F F F
attr(,"scores")
A B C D E F
7 7 0 2 1 9
Levels: C E D A B F

you can also use it in the definition of graphics:
library(lattice)
picture<-barchart(reorder(name, Freq) ~ Freq | iv, groups=dropout, data=zahlen, xlab='', stack=T, auto.key=list(text=c('Aktiv','dropout'), columns=2))

If you want to change the order of a factor without an aggregating function (e.g. if the levels have a natural order like "tiny, normal, big") read the post
Creating Factors