Skip to content

Loading matrices and other things

2 messages · Mike Schuler, Gabor Grothendieck

#
On 5/31/05, Mike Schuler <schulerm at bc.edu> wrote:
Here is something to try:

# get number of entries and read in
n <- max(count.fields("myfile.dat")) + 1
x <- scan("myfile.dat")

# create matrix from x
x.mat <- matrix(0,n,n)
x.mat[upper.tri(x.mat)] <- x
x.mat <- x.mat + t(x.mat)

# convert to distance matrix
x.dist <- as.dist(x.mat)

# run hclust
x.hclust <- hclust(x.dist)

# plot
plot(x.hclust, cex = 0.6)
rect.hclust(x.hclust,k=5,border="red")
You don't need another language.  It can all be done from R.  Suppose
we want to read in each .dat file in the current directory, plot it and
save the plot:

for (f in dir(patt = "[.]dat$")) {  x <- read.table(f); plot(x);
savePlot(f, "ps") }

savePlot, used above, is specific to Windows. See ?dev.print
if you are not on Windows.