Skip to content

Instantly share code, notes, and snippets.

@phillipjohnson
Last active December 29, 2015 05:39
Show Gist options
  • Save phillipjohnson/7623598 to your computer and use it in GitHub Desktop.
Save phillipjohnson/7623598 to your computer and use it in GitHub Desktop.
Cluster RGB data in R.
rgbdata<-read.table("data_all.txt",header=F,sep="\t")
names(rgbdata)<-c("r","b","g","count")
km<-kmeans(rgbdata[c("r","b","g")],32,iter.max=500)
rgbdata$cluster_32<-km$cluster
rgbsamp<-rgbdata[sample(nrow(rgbdata), 5000), ]
library(fpc)
plotcluster(rgbsamp[c("r","b","g")], rgbsamp$cluster_32,col=rgb(rgbsamp$r/255,rgbsamp$g/255,rgbsamp$b/255),pch=19)
library(plyr)
rgb_means<-ddply(rgbdata,~cluster_32,summarise,r=mean(r),g=mean(g),b=mean(b),count=sum(count))
rgb_means$r<-round(rgb_means$r)
rgb_means$g<-round(rgb_means$g)
rgb_means$b<-round(rgb_means$b)
rgb_means$logcount<-log(rgb_means$count,2)
write.table(rgb_means,"rgb_means.tsv",sep="\t",row.names=F)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment