Skip to content

Instantly share code, notes, and snippets.

@wulingyun
wulingyun / merge_rows.R
Created June 19, 2016 10:38
Merge matrix rows with same name
merge_rows <- function(x, method=mean, na.rm=FALSE)
{
x <- as.matrix(x[!is.na(rownames(x)), ])
r <- unique(rownames(x))
m <- length(r)
if (dim(x)[1] != m) {
y <- matrix(nrow=m, ncol=dim(x)[2])
rownames(y) <- r
colnames(y) <- colnames(x)
for (i in 1:m) {
@wulingyun
wulingyun / diffusion_kernel.R
Created June 19, 2016 10:32
Network diffusion kernel
diffusion_kernel <- function(adj, beta = 1)
{
lap <- adj - diag(rowSums(adj))
eig <- eigen(lap)
dif <- eig$vectors %*% diag(exp(beta * eig$values)) %*% t(eig$vectors)
dif
}