[R] How to erase (replace) certain elements in the data.frame?

Thomas Levine thomas.levine at gmail.com
Sun Apr 24 18:56:08 CEST 2011


As Joshua said, mine was indeed different from yours. And it didn't
work on non-numeric data. But this one seems to work right:

random.del_vec <- function (x, n.keeprows, del.percent){
  del<-function(notkeep){
    k<-floor(length(notkeep)*del.percent/100)
    notkeep[sample.int(length(notkeep),k)]<-NA
    notkeep
  }
  change<-(n.keeprows+1):nrow(x)
   x[change,]<-t(apply(x[change,],1,del))
  x
}

On the other hand, maybe you really didn't want the stratification by row.

Tom

On Sun, Apr 24, 2011 at 8:31 AM, sneaffer <sneaffer at mail.ru> wrote:
> Thanks a lot, guys.
> Thomas, your method is great, precisely the thing I've been looking forward
> to.
> Oh dear, how I love R for those list comprehension tricks!
>
> --
> View this message in context: http://r.789695.n4.nabble.com/How-to-erase-replace-certain-elements-in-the-data-frame-tp3470883p3471380.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list