R Program changes first value of matrix to 0 randomly -


i have function takes in matrix 3 columns. third column binary. if value of third column 1, replace value of same row, in first column 0.

when created function, changes value [1,1] of matrix 0 , cannot figure out issue is. help?

replace_if_miss = function(data){      s = 1     (i in data[,c(3)]) {         if (i == 1) {             data = replace(data, c(s,1), 0)         }         s = s+ 1     }      return(data) } 

for example want this:

     x                        [1,] 0.4556397 0.4040319   0  [2,] 0.8851506 0.398007    0  [3,] 0.2407926 0.006787511 1  [4,] 0.3375846 0.444135    0  [5,] 0.1898765 0.2568749   1 

to change this:

  x                        [1,] 0.4556397 0.4040319   0  [2,] 0.8851506 0.398007    0  [3,] 0         0.006787511 1  [4,] 0.3375846 0.444135    0  [5,] 0         0.2568749   1 

but changes this:

  x                          [1,] 0         0.4040319   0  [2,] 0.8851506 0.398007    0  [3,] 0         0.006787511 1  [4,] 0.3375846 0.444135    0  [5,] 0         0.2568749   1 

you can directly set value rather relying on loops:

data[data[, 3] == 1, 1] <- 0 

this setting value in column 1 0 when value in column 3 1.

i think function replacing [1, 1] 0 because when s 1, c(s, 1) c(1, 1), set 0.


Comments

Popular posts from this blog

jquery - How do you format the date used in the popover widget title of FullCalendar? -

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -