Visualizzazione post con etichetta matrix. Mostra tutti i post
Visualizzazione post con etichetta matrix. Mostra tutti i post

lunedì 4 giugno 2007

How do you get the most common row from a matrix?

If I have a matrix like this:

array(1:3,dim=c(4,5))

[,1] [,2] [,3] [,4] [,5]
[1,] 1 2 3 1 2
[2,] 2 3 1 2 3
[3,] 3 1 2 3 1
[4,] 1 2 3 1 2


in which rows 1 and 4 are similar, I want to find that vector c(1,2,3,1,2).

library(cluster)
x <- array(1:3,dim=c(4,5))
dissim <- as.matrix(daisy(as.data.frame(x)))
dissim[!upper.tri(dissim)] <- NA
unique(x[which(dissim == 0, arr.ind=TRUE), ])


or

count <- table(apply(x, 1, paste, collapse=" "))
count[which.max(count)]

martedì 15 maggio 2007

Large Matrix into a vector

mx <- matrix(rnorm(100,1:100),10,10)
vec <- c(mx)
or
vec <- c(as.matrix(mx))
or
dim(mx) <- NULL

martedì 8 maggio 2007

Fill a matrix with vectors of different lengths

v <- c(1,1,2,3,4,6)
binc <- function(x){
l <- sum(x)+1
y <- c(1,rep(0,l-1))
for (i in x) y <- y+c(rep(0,i),y)[1:l]
}
out <- lapply(1:length(v), function(i) binc(v[1:i]))
nout <- max(sapply(out, length))
sapply(out, function(x) c(x, rep(0, nout - length(x))))

martedì 17 aprile 2007

From Similarity To Distance matrix


# This function returns an object of class "dist"
sim2dist <- function(mx) as.dist(sqrt(outer(diag(mx), diag(mx), "+") - 2*mx)) # from similarity to distance matrix d.mx = as.matrix(d.mx) d.mx = sim2dist(d.mx) # The distance matrix can be used to visualize # hierarchical clustering results as dendrograms hc = hclust(d.mx) plot(hc)


See Multivariate Analysis (Probability and Mathematical Statistics) for the statistical theory.