[R] Adding NA values in random positions in a dataframe
smartpink111 at yahoo.com
Thu Nov 28 18:57:07 CET 2013
One way would be:
dat1 <- as.data.frame(matrix(sample(c(1:5,NA),50,replace=TRUE,prob=c(10,15,15,20,30,10)),ncol=5))
dat1[!is.na(dat1)][ match( sample(seq(dat1[!is.na(dat1)]),length(dat1[!is.na(dat1)])*(0.20)),seq(dat1[!is.na(dat1)]))] <- NA
Hello, I'm quite new at R so I don't know which is the most efficient
way to execute a function that I could write easily in other languages.
This is my problem: I have a dataframe with a certain numbers of
NA (approximately 10%). I want to add other NA values in random
positions of the dataframes until reaching an overall proportions of NA
values of 30% (clearly the positions with NA values don't have to
change). I tried looking at iterative function in R as apply or sapply
but I can't actually figure out how to use them in this case. Thank you.
More information about the R-help