[R] Help : delete at random

Marc Schwartz MSchwartz at MedAnalytics.com
Tue Mar 1 15:28:25 CET 2005


On Tue, 2005-03-01 at 15:04 +0100, Caroline TRUNTZER wrote:
> 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


The basic process is to randomly select row indices from the possible
number of rows. If your data frame is 'df' and you want to randomly
delete 10 rows:

df.new <- df[-sample(1:nrow(df), 10), ]

The sample() function in this case randomly selects 10 values in the
range 1:nrow(df). Using the '-' then removes these rows in the
subsetting process, returning the new, smaller, data frame in df.new.

See ?sample and ?Extract for more information.

HTH,

Marc Schwartz




More information about the R-help mailing list