replacing randomly values in an existing matrix in RPercentage removal of attribute values and replace to N/A ​from the database in RHow to make a great R reproducible exampleCheck existence of directory and create if doesn't existRemove rows with all or some NAs (missing values) in data.frameHow do I replace NA values with zeros in an R dataframe?Replace all values in a matrix <0.1 with 0Convert object list to obtain rownames Radd a value randomly to a matrixrandomly replace elements in a matrixBaum-Welch algorithm showing Log-likelihood: NaN BIC criterium: NaN AIC criterium: NaNUsing apply using multiple sources of data?Replacing Values in Randomly Generated Matrix with Additional Random Numbers

German idiomatic equivalents of 能骗就骗 (if you can cheat, then cheat)

Does friction always oppose motion?

Why are examinees often not allowed to leave during the start and end of an exam?

Odd PCB Layout for Voltage Regulator

How to track mail undetectably?

What caused the flashes in the video footage of Chernobyl?

What prevents a US state from colonizing a smaller state?

Cannot overlay, because ListPlot does not draw same X range despite the same PlotRange

Is it OK to throw pebbles and stones in streams, waterfalls, ponds, etc.?

Are there advantages in writing by hand over typing out a story?

Confusion in understanding the behavior of inductor in RL circuit with DC source

How can I change my buffer system for protein purification?

How to idiomatically express the idea "if you can cheat without being caught, do it"

Available snapshots for main net?

Sentences with no verb, but an ablative

Variable declaration inside main loop

Are the plates of a battery really charged?

Find the closest three-digit hex colour

Does the Grothendieck group of finitely generated modules form a commutative ring where the multiplication structure is induced from tensor product?

Why is the saxophone not common in classical repertoire?

Polynomial and roots problems

Blood-based alcohol for vampires?

Old story where computer expert digitally animates The Lord of the Rings

Making arrow with a gradual colour



replacing randomly values in an existing matrix in R


Percentage removal of attribute values and replace to N/A ​from the database in RHow to make a great R reproducible exampleCheck existence of directory and create if doesn't existRemove rows with all or some NAs (missing values) in data.frameHow do I replace NA values with zeros in an R dataframe?Replace all values in a matrix <0.1 with 0Convert object list to obtain rownames Radd a value randomly to a matrixrandomly replace elements in a matrixBaum-Welch algorithm showing Log-likelihood: NaN BIC criterium: NaN AIC criterium: NaNUsing apply using multiple sources of data?Replacing Values in Randomly Generated Matrix with Additional Random Numbers













2















I have an existing matrix and I want to replace some of the existing values by NA's in a random uniform way.



I tried to use the following, but it only replaced 392 values with NA, not 452 as I expected. What am I doing wrong?



N <- 452

ind1 <- (runif(N,2,length(macro_complet$Sod)))

macro_complet$Sod[ind1] <- NA

summary(macro_complet$Sod)
Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
0.3222 0.9138 1.0790 1.1360 1.3010 2.8610 392.0000


My data looks like this



> str(macro_complet)
'data.frame': 1504 obs. of 26 variables:
$ Sod : num 8.6 13.1 12 13.8 12.9 10 7 14.8 11.3 4.9 ...
$ Azo : num 2 1.7 2.2 1.9 1.89 1.61 1.72 2.1 1.63 2 ...
$ Cal : num 26 28.1 24 28.5 24.5 24 17.4 26.6 24.8 10.5 ...
$ Bic : num 72 82 81 84 77 68 66 81 70 37.8 ...
$ DBO : num 3 2.2 3 2.7 3.3 3 3.2 2.9 2.8 2 ...
$ AzoK : num 0.7 0.7 0.9 0.8 0.7 0.7 0.7 0.9 0.7 0.7 ...
$ Orho : num 0.3 0.2 0.31 0.19 0.19 0.2 0.16 0.24 0.2 0.01 ...
$ Ammo : num 0.12 0.16 0.15 0.13 0.19 0.22 0.19 0.16 0.17 0.08 ...
$ Carb : num 0.3 0.3 2 0.3 0.3 0.3 0.3 0.3 0.3 0.5 ...
$ Ox : num 10.2 9.7 9.8 9.6 9.7 9.1 9.1 8.1 9.7 10.6 ...
$ Mag : num 5.5 6.5 6.3 7 6.4 5.1 6 6.7 5.7 2 ...
$ Nit : num 4.2 4.7 5.7 4.6 4.2 3.5 4.9 4.5 4.2 2.8 ...
$ Matsu : num 17 9 24 15 17 19 20 19 13 3.9 ...
$ Tp : num 10.5 9.7 11.9 12 12.9 11.2 12.8 13.7 11.5 10.6 ...
$ Co : num 3 3.45 3.3 3.54 2.7 2.7 3.3 3.49 2.8 1.8 ...
$ Ch : num 17 24 22 28 25 19 13 28 23 6.4 ...
$ Cu : num 25 15 20 20 15 20 15 15 20 15 ...
$ Po : num 3.5 3.8 4 3.6 3.8 3.7 3 4.2 3.7 0.4 ...
$ Ph : num 0.2 0.17 0.2 0.14 0.18 0.2 0.17 0.17 0.17 0.01 ...
$ Cnd : int 226 275 285 295 272 225 267 283 251 61 ...
$ Txs : num 93 88 89 86 87 88 84 80 91 94 ...
$ Niti : num 0.06 0.09 0.07 0.06 0.08 0.07 0.08 0.11 0.1 0.01 ...
$ Dt : num 9 9.7 9 10.2 8 8 7 9.4 8.5 3 ...
$ H : num 7.6 7.7 7.6 7.7 7.55 7.4 7.3 7.5 7.5 7.6 ...
$ Dco : int 17 12 15 13 15 20 16 14 12 7 ...
$ Sf : num 22 20.5 18 22.2 22.1 21 11.6 21.7 21.9 6.8 ...



I also tried to do this for only a single variable, but got the same result.



I converted my data frame into a matrix using



as.matrix(n1)


then I replaced some values for only one variable



N <- 300

ind <- (runif(N,1,length(n1$Sodium)))

n1$Sodium[ind] <- NA


However, using summary() I observed that only 262 values were replaced instead of 300 as expected. What am I doing wrong?



summary(n1$Sodium)
Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
0.3222 0.8976 1.0790 1.1320 1.3010 2.8610 262.0000









share|improve this question




























    2















    I have an existing matrix and I want to replace some of the existing values by NA's in a random uniform way.



    I tried to use the following, but it only replaced 392 values with NA, not 452 as I expected. What am I doing wrong?



    N <- 452

    ind1 <- (runif(N,2,length(macro_complet$Sod)))

    macro_complet$Sod[ind1] <- NA

    summary(macro_complet$Sod)
    Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
    0.3222 0.9138 1.0790 1.1360 1.3010 2.8610 392.0000


    My data looks like this



    > str(macro_complet)
    'data.frame': 1504 obs. of 26 variables:
    $ Sod : num 8.6 13.1 12 13.8 12.9 10 7 14.8 11.3 4.9 ...
    $ Azo : num 2 1.7 2.2 1.9 1.89 1.61 1.72 2.1 1.63 2 ...
    $ Cal : num 26 28.1 24 28.5 24.5 24 17.4 26.6 24.8 10.5 ...
    $ Bic : num 72 82 81 84 77 68 66 81 70 37.8 ...
    $ DBO : num 3 2.2 3 2.7 3.3 3 3.2 2.9 2.8 2 ...
    $ AzoK : num 0.7 0.7 0.9 0.8 0.7 0.7 0.7 0.9 0.7 0.7 ...
    $ Orho : num 0.3 0.2 0.31 0.19 0.19 0.2 0.16 0.24 0.2 0.01 ...
    $ Ammo : num 0.12 0.16 0.15 0.13 0.19 0.22 0.19 0.16 0.17 0.08 ...
    $ Carb : num 0.3 0.3 2 0.3 0.3 0.3 0.3 0.3 0.3 0.5 ...
    $ Ox : num 10.2 9.7 9.8 9.6 9.7 9.1 9.1 8.1 9.7 10.6 ...
    $ Mag : num 5.5 6.5 6.3 7 6.4 5.1 6 6.7 5.7 2 ...
    $ Nit : num 4.2 4.7 5.7 4.6 4.2 3.5 4.9 4.5 4.2 2.8 ...
    $ Matsu : num 17 9 24 15 17 19 20 19 13 3.9 ...
    $ Tp : num 10.5 9.7 11.9 12 12.9 11.2 12.8 13.7 11.5 10.6 ...
    $ Co : num 3 3.45 3.3 3.54 2.7 2.7 3.3 3.49 2.8 1.8 ...
    $ Ch : num 17 24 22 28 25 19 13 28 23 6.4 ...
    $ Cu : num 25 15 20 20 15 20 15 15 20 15 ...
    $ Po : num 3.5 3.8 4 3.6 3.8 3.7 3 4.2 3.7 0.4 ...
    $ Ph : num 0.2 0.17 0.2 0.14 0.18 0.2 0.17 0.17 0.17 0.01 ...
    $ Cnd : int 226 275 285 295 272 225 267 283 251 61 ...
    $ Txs : num 93 88 89 86 87 88 84 80 91 94 ...
    $ Niti : num 0.06 0.09 0.07 0.06 0.08 0.07 0.08 0.11 0.1 0.01 ...
    $ Dt : num 9 9.7 9 10.2 8 8 7 9.4 8.5 3 ...
    $ H : num 7.6 7.7 7.6 7.7 7.55 7.4 7.3 7.5 7.5 7.6 ...
    $ Dco : int 17 12 15 13 15 20 16 14 12 7 ...
    $ Sf : num 22 20.5 18 22.2 22.1 21 11.6 21.7 21.9 6.8 ...



    I also tried to do this for only a single variable, but got the same result.



    I converted my data frame into a matrix using



    as.matrix(n1)


    then I replaced some values for only one variable



    N <- 300

    ind <- (runif(N,1,length(n1$Sodium)))

    n1$Sodium[ind] <- NA


    However, using summary() I observed that only 262 values were replaced instead of 300 as expected. What am I doing wrong?



    summary(n1$Sodium)
    Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
    0.3222 0.8976 1.0790 1.1320 1.3010 2.8610 262.0000









    share|improve this question


























      2












      2








      2








      I have an existing matrix and I want to replace some of the existing values by NA's in a random uniform way.



      I tried to use the following, but it only replaced 392 values with NA, not 452 as I expected. What am I doing wrong?



      N <- 452

      ind1 <- (runif(N,2,length(macro_complet$Sod)))

      macro_complet$Sod[ind1] <- NA

      summary(macro_complet$Sod)
      Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
      0.3222 0.9138 1.0790 1.1360 1.3010 2.8610 392.0000


      My data looks like this



      > str(macro_complet)
      'data.frame': 1504 obs. of 26 variables:
      $ Sod : num 8.6 13.1 12 13.8 12.9 10 7 14.8 11.3 4.9 ...
      $ Azo : num 2 1.7 2.2 1.9 1.89 1.61 1.72 2.1 1.63 2 ...
      $ Cal : num 26 28.1 24 28.5 24.5 24 17.4 26.6 24.8 10.5 ...
      $ Bic : num 72 82 81 84 77 68 66 81 70 37.8 ...
      $ DBO : num 3 2.2 3 2.7 3.3 3 3.2 2.9 2.8 2 ...
      $ AzoK : num 0.7 0.7 0.9 0.8 0.7 0.7 0.7 0.9 0.7 0.7 ...
      $ Orho : num 0.3 0.2 0.31 0.19 0.19 0.2 0.16 0.24 0.2 0.01 ...
      $ Ammo : num 0.12 0.16 0.15 0.13 0.19 0.22 0.19 0.16 0.17 0.08 ...
      $ Carb : num 0.3 0.3 2 0.3 0.3 0.3 0.3 0.3 0.3 0.5 ...
      $ Ox : num 10.2 9.7 9.8 9.6 9.7 9.1 9.1 8.1 9.7 10.6 ...
      $ Mag : num 5.5 6.5 6.3 7 6.4 5.1 6 6.7 5.7 2 ...
      $ Nit : num 4.2 4.7 5.7 4.6 4.2 3.5 4.9 4.5 4.2 2.8 ...
      $ Matsu : num 17 9 24 15 17 19 20 19 13 3.9 ...
      $ Tp : num 10.5 9.7 11.9 12 12.9 11.2 12.8 13.7 11.5 10.6 ...
      $ Co : num 3 3.45 3.3 3.54 2.7 2.7 3.3 3.49 2.8 1.8 ...
      $ Ch : num 17 24 22 28 25 19 13 28 23 6.4 ...
      $ Cu : num 25 15 20 20 15 20 15 15 20 15 ...
      $ Po : num 3.5 3.8 4 3.6 3.8 3.7 3 4.2 3.7 0.4 ...
      $ Ph : num 0.2 0.17 0.2 0.14 0.18 0.2 0.17 0.17 0.17 0.01 ...
      $ Cnd : int 226 275 285 295 272 225 267 283 251 61 ...
      $ Txs : num 93 88 89 86 87 88 84 80 91 94 ...
      $ Niti : num 0.06 0.09 0.07 0.06 0.08 0.07 0.08 0.11 0.1 0.01 ...
      $ Dt : num 9 9.7 9 10.2 8 8 7 9.4 8.5 3 ...
      $ H : num 7.6 7.7 7.6 7.7 7.55 7.4 7.3 7.5 7.5 7.6 ...
      $ Dco : int 17 12 15 13 15 20 16 14 12 7 ...
      $ Sf : num 22 20.5 18 22.2 22.1 21 11.6 21.7 21.9 6.8 ...



      I also tried to do this for only a single variable, but got the same result.



      I converted my data frame into a matrix using



      as.matrix(n1)


      then I replaced some values for only one variable



      N <- 300

      ind <- (runif(N,1,length(n1$Sodium)))

      n1$Sodium[ind] <- NA


      However, using summary() I observed that only 262 values were replaced instead of 300 as expected. What am I doing wrong?



      summary(n1$Sodium)
      Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
      0.3222 0.8976 1.0790 1.1320 1.3010 2.8610 262.0000









      share|improve this question
















      I have an existing matrix and I want to replace some of the existing values by NA's in a random uniform way.



      I tried to use the following, but it only replaced 392 values with NA, not 452 as I expected. What am I doing wrong?



      N <- 452

      ind1 <- (runif(N,2,length(macro_complet$Sod)))

      macro_complet$Sod[ind1] <- NA

      summary(macro_complet$Sod)
      Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
      0.3222 0.9138 1.0790 1.1360 1.3010 2.8610 392.0000


      My data looks like this



      > str(macro_complet)
      'data.frame': 1504 obs. of 26 variables:
      $ Sod : num 8.6 13.1 12 13.8 12.9 10 7 14.8 11.3 4.9 ...
      $ Azo : num 2 1.7 2.2 1.9 1.89 1.61 1.72 2.1 1.63 2 ...
      $ Cal : num 26 28.1 24 28.5 24.5 24 17.4 26.6 24.8 10.5 ...
      $ Bic : num 72 82 81 84 77 68 66 81 70 37.8 ...
      $ DBO : num 3 2.2 3 2.7 3.3 3 3.2 2.9 2.8 2 ...
      $ AzoK : num 0.7 0.7 0.9 0.8 0.7 0.7 0.7 0.9 0.7 0.7 ...
      $ Orho : num 0.3 0.2 0.31 0.19 0.19 0.2 0.16 0.24 0.2 0.01 ...
      $ Ammo : num 0.12 0.16 0.15 0.13 0.19 0.22 0.19 0.16 0.17 0.08 ...
      $ Carb : num 0.3 0.3 2 0.3 0.3 0.3 0.3 0.3 0.3 0.5 ...
      $ Ox : num 10.2 9.7 9.8 9.6 9.7 9.1 9.1 8.1 9.7 10.6 ...
      $ Mag : num 5.5 6.5 6.3 7 6.4 5.1 6 6.7 5.7 2 ...
      $ Nit : num 4.2 4.7 5.7 4.6 4.2 3.5 4.9 4.5 4.2 2.8 ...
      $ Matsu : num 17 9 24 15 17 19 20 19 13 3.9 ...
      $ Tp : num 10.5 9.7 11.9 12 12.9 11.2 12.8 13.7 11.5 10.6 ...
      $ Co : num 3 3.45 3.3 3.54 2.7 2.7 3.3 3.49 2.8 1.8 ...
      $ Ch : num 17 24 22 28 25 19 13 28 23 6.4 ...
      $ Cu : num 25 15 20 20 15 20 15 15 20 15 ...
      $ Po : num 3.5 3.8 4 3.6 3.8 3.7 3 4.2 3.7 0.4 ...
      $ Ph : num 0.2 0.17 0.2 0.14 0.18 0.2 0.17 0.17 0.17 0.01 ...
      $ Cnd : int 226 275 285 295 272 225 267 283 251 61 ...
      $ Txs : num 93 88 89 86 87 88 84 80 91 94 ...
      $ Niti : num 0.06 0.09 0.07 0.06 0.08 0.07 0.08 0.11 0.1 0.01 ...
      $ Dt : num 9 9.7 9 10.2 8 8 7 9.4 8.5 3 ...
      $ H : num 7.6 7.7 7.6 7.7 7.55 7.4 7.3 7.5 7.5 7.6 ...
      $ Dco : int 17 12 15 13 15 20 16 14 12 7 ...
      $ Sf : num 22 20.5 18 22.2 22.1 21 11.6 21.7 21.9 6.8 ...



      I also tried to do this for only a single variable, but got the same result.



      I converted my data frame into a matrix using



      as.matrix(n1)


      then I replaced some values for only one variable



      N <- 300

      ind <- (runif(N,1,length(n1$Sodium)))

      n1$Sodium[ind] <- NA


      However, using summary() I observed that only 262 values were replaced instead of 300 as expected. What am I doing wrong?



      summary(n1$Sodium)
      Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
      0.3222 0.8976 1.0790 1.1320 1.3010 2.8610 262.0000






      r






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 25 at 23:51









      divibisan

      6,6189 gold badges18 silver badges35 bronze badges




      6,6189 gold badges18 silver badges35 bronze badges










      asked Apr 9 '13 at 15:53









      Eva SerranoEva Serrano

      134 bronze badges




      134 bronze badges




















          2 Answers
          2






          active

          oldest

          votes


















          2














          you must apply runif in the right spot, which is the index to vec. (The way you have it now, you are asking R to draw random numbers from a uniform distribution between NA and NA, which of course does not make sense and so it gives you back NaNs)



          Try instead:



           N <- 5 # the number of random values to replace
          inds <- round ( runif(N, 1, length(vec)) ) # draw random values from [1, length(vec)]
          vec[inds] <- NA # use the random values as indicies to vec, for which to replace


          Note that it is not necessary to use round(.) since [[ will accept numerics, but they will all be rounded down by default, which is just slightly less than a uniform dist.






          share|improve this answer























          • appllying this for one specific colum it replace 336 values of the 350 i want to replace any idea of what am i doing wrong? N <- 375 inds <- (runif(N,1,length(vec$Sodium))) vec$Sodium[inds] <- NA

            – Eva Serrano
            Apr 10 '13 at 7:57












          • Eva, please see @Roman-Lustrik 's comment. You need to edit your question with your code and sample data. Alternatively, you can open a new question as a follow up to this question.

            – Ricardo Saporta
            Apr 10 '13 at 14:09


















          6














          Try this. This will sample your matrix uniformly without replacement (so the same value is not chosen and replaced twice). If you want some other distribution, you can modify the weights using the prob argument (see ?sample)



          vec <- matrix(1:25, nrow = 5)
          vec[sample(1:length(vec), 4, replace = FALSE)] <- NA

          vec
          [,1] [,2] [,3] [,4] [,5]
          [1,] NA 6 NA 16 NA
          [2,] NA 7 12 17 22
          [3,] 3 8 13 18 23
          [4,] 4 9 14 19 24
          [5,] 5 10 15 20 25





          share|improve this answer

























          • thanks, but does this generates random uniform NA's, i really need that the NA's replacing values keep a uniform way

            – Eva Serrano
            Apr 9 '13 at 16:03











          • this is not working when i use a bigger matrix (1000 rows and 26 colums) insted of replacing only 4 values it replaces all the values of 4 colums into NA's, how can i change this in order to control the number of values to be replaced?

            – Eva Serrano
            Apr 9 '13 at 16:26






          • 3





            @EvaSerrano I would rather not guess at what's happening. Edit your question to include the code you're using, please.

            – Roman Luštrik
            Apr 9 '13 at 17:07











          • so i tried using as <code> macro_complet[sample(length(macro_complet$Sodium), 250, replace =FALSE)] <- NA <code>

            – Eva Serrano
            Apr 10 '13 at 7:42












          • @EvaSerrano Can you edit your question and show us what macro_complet looks like (using str())?

            – Roman Luštrik
            Apr 10 '13 at 15:56













          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f15906689%2freplacing-randomly-values-in-an-existing-matrix-in-r%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          you must apply runif in the right spot, which is the index to vec. (The way you have it now, you are asking R to draw random numbers from a uniform distribution between NA and NA, which of course does not make sense and so it gives you back NaNs)



          Try instead:



           N <- 5 # the number of random values to replace
          inds <- round ( runif(N, 1, length(vec)) ) # draw random values from [1, length(vec)]
          vec[inds] <- NA # use the random values as indicies to vec, for which to replace


          Note that it is not necessary to use round(.) since [[ will accept numerics, but they will all be rounded down by default, which is just slightly less than a uniform dist.






          share|improve this answer























          • appllying this for one specific colum it replace 336 values of the 350 i want to replace any idea of what am i doing wrong? N <- 375 inds <- (runif(N,1,length(vec$Sodium))) vec$Sodium[inds] <- NA

            – Eva Serrano
            Apr 10 '13 at 7:57












          • Eva, please see @Roman-Lustrik 's comment. You need to edit your question with your code and sample data. Alternatively, you can open a new question as a follow up to this question.

            – Ricardo Saporta
            Apr 10 '13 at 14:09















          2














          you must apply runif in the right spot, which is the index to vec. (The way you have it now, you are asking R to draw random numbers from a uniform distribution between NA and NA, which of course does not make sense and so it gives you back NaNs)



          Try instead:



           N <- 5 # the number of random values to replace
          inds <- round ( runif(N, 1, length(vec)) ) # draw random values from [1, length(vec)]
          vec[inds] <- NA # use the random values as indicies to vec, for which to replace


          Note that it is not necessary to use round(.) since [[ will accept numerics, but they will all be rounded down by default, which is just slightly less than a uniform dist.






          share|improve this answer























          • appllying this for one specific colum it replace 336 values of the 350 i want to replace any idea of what am i doing wrong? N <- 375 inds <- (runif(N,1,length(vec$Sodium))) vec$Sodium[inds] <- NA

            – Eva Serrano
            Apr 10 '13 at 7:57












          • Eva, please see @Roman-Lustrik 's comment. You need to edit your question with your code and sample data. Alternatively, you can open a new question as a follow up to this question.

            – Ricardo Saporta
            Apr 10 '13 at 14:09













          2












          2








          2







          you must apply runif in the right spot, which is the index to vec. (The way you have it now, you are asking R to draw random numbers from a uniform distribution between NA and NA, which of course does not make sense and so it gives you back NaNs)



          Try instead:



           N <- 5 # the number of random values to replace
          inds <- round ( runif(N, 1, length(vec)) ) # draw random values from [1, length(vec)]
          vec[inds] <- NA # use the random values as indicies to vec, for which to replace


          Note that it is not necessary to use round(.) since [[ will accept numerics, but they will all be rounded down by default, which is just slightly less than a uniform dist.






          share|improve this answer













          you must apply runif in the right spot, which is the index to vec. (The way you have it now, you are asking R to draw random numbers from a uniform distribution between NA and NA, which of course does not make sense and so it gives you back NaNs)



          Try instead:



           N <- 5 # the number of random values to replace
          inds <- round ( runif(N, 1, length(vec)) ) # draw random values from [1, length(vec)]
          vec[inds] <- NA # use the random values as indicies to vec, for which to replace


          Note that it is not necessary to use round(.) since [[ will accept numerics, but they will all be rounded down by default, which is just slightly less than a uniform dist.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Apr 9 '13 at 16:59









          Ricardo SaportaRicardo Saporta

          41.7k10 gold badges117 silver badges150 bronze badges




          41.7k10 gold badges117 silver badges150 bronze badges












          • appllying this for one specific colum it replace 336 values of the 350 i want to replace any idea of what am i doing wrong? N <- 375 inds <- (runif(N,1,length(vec$Sodium))) vec$Sodium[inds] <- NA

            – Eva Serrano
            Apr 10 '13 at 7:57












          • Eva, please see @Roman-Lustrik 's comment. You need to edit your question with your code and sample data. Alternatively, you can open a new question as a follow up to this question.

            – Ricardo Saporta
            Apr 10 '13 at 14:09

















          • appllying this for one specific colum it replace 336 values of the 350 i want to replace any idea of what am i doing wrong? N <- 375 inds <- (runif(N,1,length(vec$Sodium))) vec$Sodium[inds] <- NA

            – Eva Serrano
            Apr 10 '13 at 7:57












          • Eva, please see @Roman-Lustrik 's comment. You need to edit your question with your code and sample data. Alternatively, you can open a new question as a follow up to this question.

            – Ricardo Saporta
            Apr 10 '13 at 14:09
















          appllying this for one specific colum it replace 336 values of the 350 i want to replace any idea of what am i doing wrong? N <- 375 inds <- (runif(N,1,length(vec$Sodium))) vec$Sodium[inds] <- NA

          – Eva Serrano
          Apr 10 '13 at 7:57






          appllying this for one specific colum it replace 336 values of the 350 i want to replace any idea of what am i doing wrong? N <- 375 inds <- (runif(N,1,length(vec$Sodium))) vec$Sodium[inds] <- NA

          – Eva Serrano
          Apr 10 '13 at 7:57














          Eva, please see @Roman-Lustrik 's comment. You need to edit your question with your code and sample data. Alternatively, you can open a new question as a follow up to this question.

          – Ricardo Saporta
          Apr 10 '13 at 14:09





          Eva, please see @Roman-Lustrik 's comment. You need to edit your question with your code and sample data. Alternatively, you can open a new question as a follow up to this question.

          – Ricardo Saporta
          Apr 10 '13 at 14:09











          6














          Try this. This will sample your matrix uniformly without replacement (so the same value is not chosen and replaced twice). If you want some other distribution, you can modify the weights using the prob argument (see ?sample)



          vec <- matrix(1:25, nrow = 5)
          vec[sample(1:length(vec), 4, replace = FALSE)] <- NA

          vec
          [,1] [,2] [,3] [,4] [,5]
          [1,] NA 6 NA 16 NA
          [2,] NA 7 12 17 22
          [3,] 3 8 13 18 23
          [4,] 4 9 14 19 24
          [5,] 5 10 15 20 25





          share|improve this answer

























          • thanks, but does this generates random uniform NA's, i really need that the NA's replacing values keep a uniform way

            – Eva Serrano
            Apr 9 '13 at 16:03











          • this is not working when i use a bigger matrix (1000 rows and 26 colums) insted of replacing only 4 values it replaces all the values of 4 colums into NA's, how can i change this in order to control the number of values to be replaced?

            – Eva Serrano
            Apr 9 '13 at 16:26






          • 3





            @EvaSerrano I would rather not guess at what's happening. Edit your question to include the code you're using, please.

            – Roman Luštrik
            Apr 9 '13 at 17:07











          • so i tried using as <code> macro_complet[sample(length(macro_complet$Sodium), 250, replace =FALSE)] <- NA <code>

            – Eva Serrano
            Apr 10 '13 at 7:42












          • @EvaSerrano Can you edit your question and show us what macro_complet looks like (using str())?

            – Roman Luštrik
            Apr 10 '13 at 15:56















          6














          Try this. This will sample your matrix uniformly without replacement (so the same value is not chosen and replaced twice). If you want some other distribution, you can modify the weights using the prob argument (see ?sample)



          vec <- matrix(1:25, nrow = 5)
          vec[sample(1:length(vec), 4, replace = FALSE)] <- NA

          vec
          [,1] [,2] [,3] [,4] [,5]
          [1,] NA 6 NA 16 NA
          [2,] NA 7 12 17 22
          [3,] 3 8 13 18 23
          [4,] 4 9 14 19 24
          [5,] 5 10 15 20 25





          share|improve this answer

























          • thanks, but does this generates random uniform NA's, i really need that the NA's replacing values keep a uniform way

            – Eva Serrano
            Apr 9 '13 at 16:03











          • this is not working when i use a bigger matrix (1000 rows and 26 colums) insted of replacing only 4 values it replaces all the values of 4 colums into NA's, how can i change this in order to control the number of values to be replaced?

            – Eva Serrano
            Apr 9 '13 at 16:26






          • 3





            @EvaSerrano I would rather not guess at what's happening. Edit your question to include the code you're using, please.

            – Roman Luštrik
            Apr 9 '13 at 17:07











          • so i tried using as <code> macro_complet[sample(length(macro_complet$Sodium), 250, replace =FALSE)] <- NA <code>

            – Eva Serrano
            Apr 10 '13 at 7:42












          • @EvaSerrano Can you edit your question and show us what macro_complet looks like (using str())?

            – Roman Luštrik
            Apr 10 '13 at 15:56













          6












          6








          6







          Try this. This will sample your matrix uniformly without replacement (so the same value is not chosen and replaced twice). If you want some other distribution, you can modify the weights using the prob argument (see ?sample)



          vec <- matrix(1:25, nrow = 5)
          vec[sample(1:length(vec), 4, replace = FALSE)] <- NA

          vec
          [,1] [,2] [,3] [,4] [,5]
          [1,] NA 6 NA 16 NA
          [2,] NA 7 12 17 22
          [3,] 3 8 13 18 23
          [4,] 4 9 14 19 24
          [5,] 5 10 15 20 25





          share|improve this answer















          Try this. This will sample your matrix uniformly without replacement (so the same value is not chosen and replaced twice). If you want some other distribution, you can modify the weights using the prob argument (see ?sample)



          vec <- matrix(1:25, nrow = 5)
          vec[sample(1:length(vec), 4, replace = FALSE)] <- NA

          vec
          [,1] [,2] [,3] [,4] [,5]
          [1,] NA 6 NA 16 NA
          [2,] NA 7 12 17 22
          [3,] 3 8 13 18 23
          [4,] 4 9 14 19 24
          [5,] 5 10 15 20 25






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Mar 25 at 23:52









          divibisan

          6,6189 gold badges18 silver badges35 bronze badges




          6,6189 gold badges18 silver badges35 bronze badges










          answered Apr 9 '13 at 15:56









          Roman LuštrikRoman Luštrik

          52k21 gold badges118 silver badges164 bronze badges




          52k21 gold badges118 silver badges164 bronze badges












          • thanks, but does this generates random uniform NA's, i really need that the NA's replacing values keep a uniform way

            – Eva Serrano
            Apr 9 '13 at 16:03











          • this is not working when i use a bigger matrix (1000 rows and 26 colums) insted of replacing only 4 values it replaces all the values of 4 colums into NA's, how can i change this in order to control the number of values to be replaced?

            – Eva Serrano
            Apr 9 '13 at 16:26






          • 3





            @EvaSerrano I would rather not guess at what's happening. Edit your question to include the code you're using, please.

            – Roman Luštrik
            Apr 9 '13 at 17:07











          • so i tried using as <code> macro_complet[sample(length(macro_complet$Sodium), 250, replace =FALSE)] <- NA <code>

            – Eva Serrano
            Apr 10 '13 at 7:42












          • @EvaSerrano Can you edit your question and show us what macro_complet looks like (using str())?

            – Roman Luštrik
            Apr 10 '13 at 15:56

















          • thanks, but does this generates random uniform NA's, i really need that the NA's replacing values keep a uniform way

            – Eva Serrano
            Apr 9 '13 at 16:03











          • this is not working when i use a bigger matrix (1000 rows and 26 colums) insted of replacing only 4 values it replaces all the values of 4 colums into NA's, how can i change this in order to control the number of values to be replaced?

            – Eva Serrano
            Apr 9 '13 at 16:26






          • 3





            @EvaSerrano I would rather not guess at what's happening. Edit your question to include the code you're using, please.

            – Roman Luštrik
            Apr 9 '13 at 17:07











          • so i tried using as <code> macro_complet[sample(length(macro_complet$Sodium), 250, replace =FALSE)] <- NA <code>

            – Eva Serrano
            Apr 10 '13 at 7:42












          • @EvaSerrano Can you edit your question and show us what macro_complet looks like (using str())?

            – Roman Luštrik
            Apr 10 '13 at 15:56
















          thanks, but does this generates random uniform NA's, i really need that the NA's replacing values keep a uniform way

          – Eva Serrano
          Apr 9 '13 at 16:03





          thanks, but does this generates random uniform NA's, i really need that the NA's replacing values keep a uniform way

          – Eva Serrano
          Apr 9 '13 at 16:03













          this is not working when i use a bigger matrix (1000 rows and 26 colums) insted of replacing only 4 values it replaces all the values of 4 colums into NA's, how can i change this in order to control the number of values to be replaced?

          – Eva Serrano
          Apr 9 '13 at 16:26





          this is not working when i use a bigger matrix (1000 rows and 26 colums) insted of replacing only 4 values it replaces all the values of 4 colums into NA's, how can i change this in order to control the number of values to be replaced?

          – Eva Serrano
          Apr 9 '13 at 16:26




          3




          3





          @EvaSerrano I would rather not guess at what's happening. Edit your question to include the code you're using, please.

          – Roman Luštrik
          Apr 9 '13 at 17:07





          @EvaSerrano I would rather not guess at what's happening. Edit your question to include the code you're using, please.

          – Roman Luštrik
          Apr 9 '13 at 17:07













          so i tried using as <code> macro_complet[sample(length(macro_complet$Sodium), 250, replace =FALSE)] <- NA <code>

          – Eva Serrano
          Apr 10 '13 at 7:42






          so i tried using as <code> macro_complet[sample(length(macro_complet$Sodium), 250, replace =FALSE)] <- NA <code>

          – Eva Serrano
          Apr 10 '13 at 7:42














          @EvaSerrano Can you edit your question and show us what macro_complet looks like (using str())?

          – Roman Luštrik
          Apr 10 '13 at 15:56





          @EvaSerrano Can you edit your question and show us what macro_complet looks like (using str())?

          – Roman Luštrik
          Apr 10 '13 at 15:56

















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f15906689%2freplacing-randomly-values-in-an-existing-matrix-in-r%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

          SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

          은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현