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