[R] WG: R-Problem

Sarah Goslee sarah.goslee at gmail.com
Tue Jun 21 18:32:23 CEST 2011


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:
>
>
> 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
>
> Which function is appropriate here?
> Cheers
> Niels
>


-- 
Sarah Goslee
http://www.functionaldiversity.org



More information about the R-help mailing list