Reputation: 65
I'm struggling trying to create a data.frame of random samples. I give a reproducible example:
I have a few custom functions similar to this:
vendorSelector <- function() {
sample(c("Paco", "Juan", "Alex", "Marc"), 1)
}
productSelector <- function() {
sample(c("Water", "Oil", "Carbon", "Wood"), 1)
}
I achieved to create the data frame manually doing this:
data <- data.frame(Vendor = vendorSelector(), Product = productSelector(),
stringsAsFactors = FALSE)
And then repeating manually this line of code:
data <- rbind(data, c(Vendor = vendorSelector(), Product = productSelector()))
My problem is that I want to generate a data.frame of 1000 rows and I don't want to run manually 999 times the rbind chunk to achieve it. Can you help me to think some sort of loop or custom function to achieve it? Thanks.
Upvotes: 0
Views: 164
Reputation: 3888
directly sample the 1000 rows in one go, you need to set replace=T
as the number of sampled items is bigger than the original set of items
data.frame(Vendor = sample(c("Paco", "Juan", "Alex", "Marc"), 1000, replace=T), Product = sample(c("Water", "Oil", "Carbon", "Wood"), 1000, replace=T),
stringsAsFactors = FALSE)
Upvotes: 1
Reputation: 784
You can change your functions by including a parameter and then sample n observations:
productSelector <- function(n) {
sample(c("Water", "Oil", "Carbon", "Wood"), n, replace = T)
}
vendorSelector <- function(n) {
sample(c("Paco", "Juan", "Alex", "Marc"), n, replace = T)
}
data <- data.frame(Vendor = vendorSelector(10), Product = productSelector(10),
stringsAsFactors = FALSE)
data
Vendor Product
1 Paco Oil
2 Paco Carbon
3 Juan Oil
4 Paco Oil
5 Marc Carbon
6 Juan Oil
7 Juan Carbon
8 Juan Wood
9 Alex Oil
10 Paco Wood
Upvotes: 1