[R] replace cells with column number

Dimitris Rizopoulos d.rizopoulos at erasmusmc.nl
Tue Apr 13 16:24:27 CEST 2010


supposing that these data are in a matrix, try this:

# you original data
M <- rbind(c(0,1,0,1), c(0,0,0,1), c(1,0,0,1))

V <- M # we make a copy...
V[V == 1] <- col(V)[V == 1]
V


I hope it helps.

Best,
Dimitris


On 4/13/2010 2:07 PM, burgundy wrote:
>
> Hello,
>
> I'm trying to replace cells with value 1 with the number of the column.
> Example below.
> Thank you!
>
> from:
> 0,1,0,1
> 0,0,0,1
> 1,0,0,1
>
> to:
> 0,2,0,4
> 0,0,0,4
> 1,0,0,4
>
>
>

-- 
Dimitris Rizopoulos
Assistant Professor
Department of Biostatistics
Erasmus University Medical Center

Address: PO Box 2040, 3000 CA Rotterdam, the Netherlands
Tel: +31/(0)10/7043478
Fax: +31/(0)10/7043014



More information about the R-help mailing list