jeudi 10 novembre 2016

Create a column of random integers, where the maximum integer for each row comes from an adjacent column

I have a data frame:

letter <- c("A", "B", "C")
min <- c(1, 2, 3)
max <- c(4, 5, 6)
df <- data.frame(letter, number)

I want to add a third column to df which generates a random number for each row, where the lower and upper limits from which the random integers are sampled from are the min and max values of that row, respectively.

I have an inkling that I should use sample(), but when I do it returns a column of values drawn randomly from the referenced column:

df$random <- sample(df$max)

Could I also use runif?




Aucun commentaire:

Enregistrer un commentaire