Skip to content

WG: R-Problem

3 messages · Bleicher Niels (AFS), Sarah Goslee, Alexander Engelhardt

#
cut() and table() will do it. Here's one less-than-elegant way to put
them together:

test <- structure(list(X1 = c(0, 1.3, 1.6, 2.1), X2 = c(1.3, 0, 1.9,
1.7), X3 = c(1.6, 1.9, 0, 2.2), X4 = c(2.1, 1.7, 2.2, 0)), .Names = c("X1",
"X2", "X3", "X4"), class = "data.frame", row.names = c("1", "2",
"3", "4"))

test1 <- apply(test, 2, cut, breaks=seq(0, 2.5, by=.5),
labels=c("0.5", "1", "1.5", "2", "2.5"))

apply(test1, 2, function(x)table(factor(x, levels=c("0.5", "1", "1.5",
"2", "2.5"))))

Sarah


On Tue, Jun 21, 2011 at 10:43 AM, Bleicher Niels (AFS)
<niels.bleicher at zuerich.ch> wrote:

  
    
#
Am 21.06.2011 16:43, schrieb Bleicher Niels (AFS):
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