Try this:

df <- as.data.frame(mapply(`[<-`, x = df, i = v, NA))


On Sat, May 8, 2010 at 12:12 PM, <soeren.vogel@eawag.ch> wrote:

> Hello, after the creation of a data.frame I like to add NAs as follows:
>
> n <- 743;
> x <- runif(n, 1, 7);
> Y <- runif(n, 1, 7);
> Ag6 <- runif(n, 1, 7);
> df <- data.frame(x, Y, Ag6);
> # a list with positions:
> v <- apply(df, 2, function(x) sample(n, sample(1:ceiling(5*n/100), 1),
> repl=F));
> # a loop too much?
> for (i in 1:length(df)){
>  df[unlist(v[i]), i] <- NA;
> }
> summary(df);
>
> This works fine but it appears to me that there is a more elegant and
> simple way -- which one?
>
> Thanks
> Sören
>
> ______________________________________________
> R-help@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.
>



-- 
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O

	[[alternative HTML version deleted]]

