Re: [R] Adding NA values in random positions in a dataframe

2013-11-29 Thread Bert Gunter
An essentially identical approach that may be a tad clearer -- but requires additional space -- first creates a logical vector for the locations of the NA's in the unlisted data.frame. Further NA positions are randomly added and then the augmented vector is used as a logical matrix to index where t

Re: [R] Adding NA values in random positions in a dataframe

2013-11-29 Thread arun
Hi, I used that because 10% of the values in the data were already NA. You are right.  Sorry, ?match() is unnecessary.  I was trying another solution with match() which didn't work out and forgot to check whether it was adequate or not. set.seed(49) dat1[!is.na(dat1)][sample(seq(dat1[!is.na(da

Re: [R] Adding NA values in random positions in a dataframe

2013-11-28 Thread arun
Hi, One way would be:  set.seed(42)  dat1 <- as.data.frame(matrix(sample(c(1:5,NA),50,replace=TRUE,prob=c(10,15,15,20,30,10)),ncol=5)) set.seed(49)  dat1[!is.na(dat1)][ match( sample(seq(dat1[!is.na(dat1)]),length(dat1[!is.na(dat1)])*(0.20)),seq(dat1[!is.na(dat1)]))] <- NA length(dat1[is.na(dat1