one way is:
x <- matrix(c(10,20,30,40,0,
10,10,10,10,50,
30,30,30,5,5), nrow=3, ncol=5, byrow=TRUE,
dimnames = list(1:3, letters[1:5])
)
ind <- combn(ncol(x), 2)
out <- apply(ind, 2, function (i) as.numeric(x[, i[1]] >= x[, i[2]]))
colnames(out) <- apply(ind, 2, function (i, nams)
paste(nams[i[1]], nams[i[2]], sep = ""), nams = colnames(x))
out
I hope it helps.
Best,
Dimitris
Iuri Gavronski wrote:> Hi,
>
> I need to convert a ranking dataset (actually, it is an ipsative
> scale) to paired-comparisons.
>
> For example, if I have this data:
> x <- matrix(c(10,20,30,40,0,
> 10,10,10,10,50,
> 30,30,30,5,5), nrow=3, ncol=5, byrow=TRUE,
> dimnames = list(1:3, letters[1:5])
> )
>
> For every pair of data, I would need to apply a transformation like:
>
> if a < b, ab = 0, otherwise ab=1
>
> In the end, I need a matrix like that:
>
> ab ac ad ae bc bd be cd ce de
> 0 0 0 1 0 0 1 0 1 1
> 1 1 1 0 1 1 0 1 0 0
> 1 1 1 1 1 1 1 1 1 1
>
> Would anyone suggest a "no loops" way for doing that?
>
> By the way, I would need to apply a tetrachoric correlation to this
> output matrix. Should I use package polycor? Any thoughts would be
> welcome.
>
> Thanks,
>
> Iuri.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
--
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus Medical Center
Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014