hkhan
January 3, 2021, 10:28pm
1
Hi,
I keep receiving this error when i use the sample():
*Error in sample.int(length(x), size,replace, prob) : *
incorrect number of probabilities*
This is what I am doing:
prod <- sample(prod_table$key, no_of_recs, replace=T, prob=c(1, 2,3,4,5,6))
unit <- sample(c(1,2), no_of_recs, replace=T, prob=c(10, 3))
Please let me know what I'm doing wrong.
Thanks!!
Does prod_table$key
have 6 values? Also, your probabilities should sum up to 1.
If you share your data (or a sample) then we can help you get your desired result.
hkhan:
prod_table$key
it depends on what this is.
is it a 6 long vector ?
hkhan
January 3, 2021, 10:32pm
4
prod_table has four values :
prod_table <-
data.frame(key=c("Washing Machine", "Frudge", "Vacuum Cleaner", "Microwave Oven"),
price=c(200, 500, 400, 150),
stringsAsFactors = TRUE)
hkhan
January 3, 2021, 10:32pm
5
prod_table has four values :
prod_table <-
data.frame(key=c("Washing Machine", "Frudge", "Vacuum Cleaner", "Microwave Oven"),
price=c(200, 500, 400, 150),
stringsAsFactors = TRUE)
[/quote]
hkhan:
prob=c(1, 2,3,4,5,6)
that being the case, what was your intention when you chose these values ?
hkhan
January 3, 2021, 10:35pm
7
somewhere else i had six variables - i think i muddled it up here and thought it was referencing that. but now i have changed it to four and yet i still get the same error
hkhan:
data.frame(key=c("Washing Machine", "Frudge", "Vacuum Cleaner", "Microwave Oven"),
price=c(200, 500, 400, 150),
stringsAsFactors = TRUE)
If that is the case, then your prob
argument needs to contain 4 values: one probability for each value. Also make sure that they sum up to one. Here is an example:
prod_table <- data.frame(key=c("Washing Machine", "Frudge", "Vacuum Cleaner", "Microwave Oven"),
price=c(200, 500, 400, 150),
stringsAsFactors = TRUE)
sample(prod_table$key, no_of_recs, replace=T, prob=c(0.2, 0.1, 0.4, 0.3))
no_of_recs <- 3
prod_table <-
data.frame(key=c("Washing Machine", "Frudge", "Vacuum Cleaner", "Microwave Oven"),
price=c(200, 500, 400, 150),
stringsAsFactors = TRUE)
set.seed(42)
prod <- sample(prod_table$key, no_of_recs, replace=T, prob=c(1, 2,3,4)/10)
unit <- sample(c(1,2), no_of_recs, replace=T, prob=c(10, 3)/13)
prod
unit
I don't get any errors ...
>prod
1] Washing Machine Washing Machine Microwave Oven
Levels: Frudge Microwave Oven Vacuum Cleaner Washing Machine
> unit
[1] 2 1 1
hkhan
January 3, 2021, 10:39pm
10
yes, thank you- this works!
hkhan
January 3, 2021, 10:40pm
11
yeah i wasn't getting it to add up to one, hence my issue. Thank you it now works!
system
Closed
January 10, 2021, 10:41pm
12
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed. If you have a query related to it or one of the replies, start a new topic and refer back with a link.