# Error incorrect number of probabilities

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.

it depends on what this is.
is it a 6 long vector ?

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)
``````

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]

that being the case, what was your intention when you chose these values ?

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

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``````

yes, thank you- this works!

yeah i wasn't getting it to add up to one, hence my issue. Thank you it now works!

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.