[R] Help : delete at random

TEMPL Matthias Matthias.Templ at statistik.gv.at
Tue Mar 1 15:52:41 CET 2005


Hello,

d <- data.frame(a=c(2,3,4), b=c(2,4,1), c=c(3,5,6))

## one NA

s.r <- sample(dim(d)[1], 1)
s.c <- sample(dim(d)[2], 1)

d.na <- d
d.na[s.r, s.c] <- NA
d.na

# Here a matrix is more comfortable by using sample.

For multiple NA, you should write a loop, but to choose e.g. exact 4 values, it might be, that one value is more than one times chosen.
For this purpose you can search at your NA by using which(is.na(d), arr.ind=TRUE) and count only if those index is not a NA at a while loop. This is not an elegant way, but probably it helps you a little bit.

Best,
Matthias



> -----Ursprüngliche Nachricht-----
> Von: r-help-bounces at stat.math.ethz.ch 
> [mailto:r-help-bounces at stat.math.ethz.ch] Im Auftrag von 
> Caroline TRUNTZER
> Gesendet: Dienstag, 01. März 2005 15:04
> An: R-help at stat.math.ethz.ch
> Betreff: [R] Help : delete at random
> 
> 
> Hello
> I would like to delete some values at random in a data frame. 
> Does anyone know how I could do? With best regards Caroline
> 
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list 
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read 
> the posting guide! http://www.R-project.org/posting-guide.html
>




More information about the R-help mailing list