Posts tagged with factor

1 comments

A quicky..

If you’re (and you should) interested in principal components then take a good look at this. The linked post will take you by hand to do everything from scratch. If you’re not in the mood then the dollowing R functions will help you.

An example.

# Generates sample matrix of five discrete clusters that have
# very different mean and standard deviation values.
z1 <- rnorm(10000, mean=1, sd=1);
z2 <- rnorm(10000, mean=3, sd=3);
z3 <- rnorm(10000, mean=5, sd=5);
z4 <- rnorm(10000, mean=7, sd=7);
z5 <- rnorm(10000, mean=9, sd=9);
mydata <- matrix(c(z1, z2, z3, z4, z5), 2500, 20, byrow=T,
dimnames=list(paste("R", 1:2500, sep=""), paste("C", 1:20, sep="")))

# Performs principal component analysis after scaling the data.
# It returns a list with class "prcomp" that contains five components:
#   (1) the standard deviations (sdev) of the principal components,
#   (2) the matrix of eigenvectors (rotation),
#   (3) the principal component data (x),
#   (4) the centering (center) and
#   (5) scaling (scale) used.
pca <- prcomp(mydata, scale=T)
 Read the rest of this entry »

Instead of the factor() function which usually applies after defining a vector there’s the gl() base function to do this in one step, eg

freq <- c(204,6,1,211,13,5,357,44,38,92,34,49)
row <- gl(4,3,length=12)
col <- gl(3,1,length=12)
> col
[1] 1 2 3 1 2 3 1 2 3 1 2 3
Levels: 1 2 3
tt <- data.frame(freq,row,col)
> xtabs(tt)
col
row   1   2   3
 1 204   6   1
 2 211  13   5
 3 357  44  38
 4  92  34  49
Real Time Web Analytics