Randomly Replace Values with NA

I am working the R programming language. I am trying select 10% of the elements in my dataset (excluding elements in the first column) and replace them with NA. I tried to do this with the following code:

 library(longitudinalData)
 data(artificialLongData)

second_dataset = artificialLongData
second_dataset[sample(nrow(second_dataset),0.1*nrow(second_dataset ))]<- NA

This produces the following error:

Error in `[<-.data.frame`(`*tmp*`, sample(nrow(second_dataset), 0.1 *  : 
  new columns would leave holes after existing columns

Can someone please show me how to fix this problem?

Thanks!

Note: The final result should look something like this:

  id    t0    t1    t2    t3    t4    t5    t6    t7    t8    t9   t10
1 s1  NA  NA -1.85 -2.05  1.01  1.56  NA  0.52 -0.06 -1.09  0.44
2 s2 -4.88 -2.95 -2.38  3.73 -2.77  1.72 -0.99 -0.70  NA  2.38 -0.72
3 s3  NA -0.86  NA -2.04 -1.18  4.89 NA  0.50  4.90 -0.52  NA

Here is a solution where I assume I wanted to NA 10% of values in a hiris dataset I make.

library(tidyverse)

(hiris <- head(iris, n = 10))
(dh <- dim(hiris))
set.seed(4)

cell_nums <- sample.int(prod(dh),
                        size = prod(dh) * .1,
                        replace = FALSE)

# function to find the across/down position of an element in a grid/array/matrix/dataframe
#nr is number of rows over which the data is distributed
posm <- function(n, nr) {
  tibble(
    across = ((n - 1) %/% nr + 1),
    down = ((n - 1) %% nr + 1)
  )
}

(p2na <- map_dfr(cell_nums, 
                 ~ posm(.x, nr = 10)))

for (i in seq_along(p2na$down)) {
  hiris[p2na$down[i], 
        p2na$across[i]] <- NA
}



hiris
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.