I have the following matrix
x <- matrix(c(2, 4, 3, 5, 1, 5, 7, 9, 3, 7), nrow=5, ncol=2,)
colnames(x) = c("Y","Z")
x <-data.frame(y)
I am trying to create a random number in each row where the upper limit is a number based on a variable value (in this case 1*Y based on each row's value for for Y)
I currently have:
samp<-function(x){
sample(0:1,1,replace = TRUE)}
x$randoms <- apply(y,1,samp)
which work works well applying the sample function independently to each row, but I always get an error when I try to alter the x in sample. I thought I could do something like this:
samp<-function(x){
sample(0:x$Y,1,replace = TRUE)}
x$randoms <- apply(y,1,samp)
but I guess that was wishful thinking.
Ultimately I want the result:
X Z randoms
2 5 4
4 7 7
3 9 3
5 3 1
1 7 6
Any ideas?
Aucun commentaire:
Enregistrer un commentaire