[R] Adding NAs to data.frame
soeren.vogel at eawag.ch
soeren.vogel at eawag.ch
Sat May 8 17:12:49 CEST 2010
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
More information about the R-help
mailing list