0

I have a matrix with of 1's and 0's. I want to replace all the 1's by an identifier code for that row (the identifier code is given in row 2)

I tried:

dax[,2:109] <- replace(dax[,2:109],dax[,2:109]==1,dax[2,])

but this isn't working right. I've tried to set up a loop, but I've had no success so far.

I'm new to R. Any help is appreciated

Jilber Urbina
  • 58,147
  • 10
  • 114
  • 138
lever
  • 664
  • 1
  • 5
  • 10
  • So, 1s and 0s to replace are from rows 3 onwards? – Marc in the box Nov 10 '13 at 14:59
  • 1
    Can you provide a [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)? What "isn't working right" about this? – gung - Reinstate Monica Nov 10 '13 at 15:00
  • Yes, Marc, the 1s and 0s are from row 3 onwards. Here is an example: – lever Nov 10 '13 at 21:23
  • vector <- c(500,501, 502,999,866,878,101,0,1,102,0,1,103,1,1) mymatrix <- matrix(vector, nrow=5 , ncol=3,byrow=TRUE) mymatrix mymatrix[,2:3] <- replace(mymatrix[,2:3],mymatrix[,2:3]==1,mymatrix[2,]) mymatrix – lever Nov 10 '13 at 21:23
  • sorry about the formatting. trying to figure out how to post my code of the reproducible example – lever Nov 10 '13 at 21:35

1 Answers1

0

This may do it for you, although it'd be nice to get more details from you.

for(j in 2:109) dax[dax[,j]==1,j] <- dax[2,j]
Carl Witthoft
  • 20,573
  • 9
  • 43
  • 73