[R] WG: R-Problem

Alexander Engelhardt alex at chaotic-neutral.de
Tue Jun 21 18:42:56 CEST 2011


Am 21.06.2011 16:43, schrieb Bleicher Niels (AFS):
>
>
> Dear forumites
>
> As a newbie I try to figure out whether R can do a certain job quicker than other programs and it seems so, but I don't find a solution to a seemingly simple problem:
>
> I have built a matrix of distance with as.matrix(dist()) with several hundred rows and columns
>              1          2          3          4
> 1          0          1.3       1.6       2.1
> 2          1.3       0          1.9       1.7
> 3          1.6       1.9       0          2.2
> 4          2.1       1.7       2.2       0
>
> Now I need for every column a frequency table of the type
>
> Class.  1          2          3          4
> 0.5       0          0          0          0
> 1          0          0          0          0
> 1.5       1          1          0          0
> 2          1          2          2          1
> 2.5       1          0          1          2

Hi,

I used apply() with hist(). For some reason the breakpoints had to start 
at -0.5. Try this:

x <- matrix(c(0,1.3,1.6,2.1,1.3,0,1.9,1.7,1.6,1.9,0,2.2,2.1,1.7,2.2,0), 
nrow=4)
cutpoints <- seq(-0.5, 2.5, by=0.5)
mx <- apply(x,2,function(x,...) hist(x,plot=FALSE, breaks=cutpoints)$counts)
rownames(x) <- cutpoints[-1]
colnames(x) <- 1:ncol(x)
x

Best,
  Alex



More information about the R-help mailing list