Reputation: 3
I am trying to create a random number that is between 3 and a number provided by a column max.
Create a sample data frame for reproduction.
ID <- c(1:10)
max <- c(5,3,7,6,10,4,3,3,5,6)
df <- data.frame(ID,max)
I want a third column that is a random number between 3 and max.
ID max
1 5
2 3
3 7
4 6
5 10
6 4
7 3
8 3
9 5
10 6
I have tried many variations of the following.
for(i in 1:nrow(df)){
df$rand[i] <- sample((3:df$max[i]),1,replace=TRUE)
}
The problem is the random numbers are not with in the bounds of 3 and max. See row 8. I set my seed to 1234.
ID max rand
1 1 5 3
2 2 3 3
3 3 7 4
4 4 6 4
5 5 10 5
6 6 4 4
7 7 2 3
8 8 3 2
9 9 5 4
10 10 6 3
Thanks for any help here. I am still learning R and struggling with code.
Upvotes: 0
Views: 89
Reputation: 92282
You could also keep using sample
just with some constrains (and avoid using floor
or round
)
df$rand <- sapply(seq_len(dim(df)[1]), function(x) if(df$max[x] > 3) sample(3:df$max[x], 1, replace = T) else 3)
## ID max rand
##1 1 5 3
##2 2 3 3
##3 3 7 6
##4 4 6 5
##5 5 10 7
##6 6 4 4
##7 7 2 3
##8 8 3 3
##9 9 5 4
##10 10 6 3
Upvotes: 0
Reputation: 121568
Another option using transform
and take care of the case where the max < 3:
transform(dat,y = sapply(max,function(x)floor(runif(1,3,if(x>3)x else 3))))
ID max y
1 1 5 4
2 2 3 3
3 3 7 6
4 4 6 4
5 5 10 9
6 6 4 3
7 7 2 3
8 8 3 3
9 9 5 3
10 10 6 3
Upvotes: 0
Reputation: 9344
df$rand <- sapply(df$max, function(max) round(runif(1, 3, max)))
Your 7th row has max = 2
which is why we get a NaN
.
ID max rand
1 1 5 4
2 2 3 3
3 3 7 4
4 4 6 4
5 5 10 3
6 6 4 4
7 7 2 NaN
8 8 3 3
9 9 5 4
10 10 6 4
Upvotes: 1