This works, but there might be a better way: misscols<-lapply(howmanyMiss, function(x) sample(1:n, x)) for (i in 1:nMiss){ for (j in misscols[[i]]){ X[idMiss[i],j]<-NA } }