R 更改掷骰子的值

R 更改掷骰子的值,r,sample,dice,R,Sample,Dice,假设我掷5个骰子 模拟转鼓的代码是 Rolls如果我理解正确,应该可以: n1){ 额外滚动如果我理解正确,应该可以: n1){ 额外滚动没有循环的解决方案可以是: condition = which(Rolls==6) if(length(condition)>=3){ Rolls[condition[3:length(condition)]] = sample(1:5, length(condition)-2, replace=TRUE) } 条件以6个位置说明转鼓中的位置,如果

假设我掷5个骰子

模拟转鼓的代码是


Rolls如果我理解正确,应该可以:

n1){

额外滚动如果我理解正确,应该可以:

n1){

额外滚动没有循环的解决方案可以是:

condition = which(Rolls==6)
if(length(condition)>=3){
  Rolls[condition[3:length(condition)]] = sample(1:5, length(condition)-2, replace=TRUE)
}
条件
以6个位置说明转鼓中的位置,如果超过2个,则选择第三个转鼓[条件[3:长度(条件)]
并重新取样

第二个问题可能是这样的:

remove = 3
Rolls = Rolls[-which(Rolls==remove)[1]]
如果你愿意,你可以很容易地把它们转换成函数

编辑1

为了使第二个答案更具交互性,您可以为其构建一个函数:

remove.roll = function(remove, rolls){
   rolls = rolls[-which(rolls==remove)[1]]}
然后用户可以使用他喜欢的
remove
调用函数。您还可以制作一个程序,从提示符中获取信息:

remove = readline(prompt="Enter number to remove: ")
print(Rolls = Rolls[-which(Rolls==remove)[1]])

没有循环的解决方案可以是:

condition = which(Rolls==6)
if(length(condition)>=3){
  Rolls[condition[3:length(condition)]] = sample(1:5, length(condition)-2, replace=TRUE)
}
条件
以6个位置说明转鼓中的位置,如果超过2个,则选择第三个转鼓[条件[3:长度(条件)]
并重新取样

第二个问题可能是这样的:

remove = 3
Rolls = Rolls[-which(Rolls==remove)[1]]
如果你愿意,你可以很容易地把它们转换成函数

编辑1

为了使第二个答案更具交互性,您可以为其构建一个函数:

remove.roll = function(remove, rolls){
   rolls = rolls[-which(rolls==remove)[1]]}
然后用户可以使用他喜欢的
remove
调用函数。您还可以制作一个程序,从提示符中获取信息:

remove = readline(prompt="Enter number to remove: ")
print(Rolls = Rolls[-which(Rolls==remove)[1]])

我们可以将此作为
scan()
用例的有趣演示。您可以输入要替换的值的位置。请注意,您需要逐个手动
scan()
每个位置值,并在每个位置值之后按enter键,最后您可以通过交一个空字符串
来结束输入
并按enter键

代码

dice.roll <- function(){
  # Initial toss
  Rolls <- sample(seq(1, 6), 5, replace=TRUE)
  
  # Communicate
  cat("The outcome of the dice roll was:", "\n\n", Rolls, "\n\n", 
      "would you like to reroll any of those values ?", "\n",
      "If yes enter the positions of the values you would \n like to replace, else just input an empty string.")
  
  # Take input 
  tmp1 <- scan(what = "")
  
  # Replace
  Rolls[as.numeric(tmp1)] <- sample(seq(1, 6), length(tmp1), replace=TRUE)
  
  # Return
  cat("You succesfully replaced", length(tmp1), "elements. Your rolls now look as follows: \n\n", 
      Rolls)
}

dice.roll()

# The outcome of the dice Roll was: 
#
#  6 4 6 3 4 
#
#  would you like to reroll any of those values ? 
#  If yes enter the positions of the values you would 
#  like to replace, else just input an empty string.
# 1: 1
# 2: 3
# 3: ""
# Read 2 items
# You succesfully replaced 2 elements. Your set now looks as follows 
#
#  2 4 2 3 4

dice.roll我们可以让这成为一个有趣的
scan()
用例演示。您可以输入要替换的值的位置。请注意,您需要逐个手动
scan()
每个位置值,并在每个位置值之后按enter键,最后您可以通过交出一个空字符串来结束输入
并按enter键

代码

dice.roll <- function(){
  # Initial toss
  Rolls <- sample(seq(1, 6), 5, replace=TRUE)
  
  # Communicate
  cat("The outcome of the dice roll was:", "\n\n", Rolls, "\n\n", 
      "would you like to reroll any of those values ?", "\n",
      "If yes enter the positions of the values you would \n like to replace, else just input an empty string.")
  
  # Take input 
  tmp1 <- scan(what = "")
  
  # Replace
  Rolls[as.numeric(tmp1)] <- sample(seq(1, 6), length(tmp1), replace=TRUE)
  
  # Return
  cat("You succesfully replaced", length(tmp1), "elements. Your rolls now look as follows: \n\n", 
      Rolls)
}

dice.roll()

# The outcome of the dice Roll was: 
#
#  6 4 6 3 4 
#
#  would you like to reroll any of those values ? 
#  If yes enter the positions of the values you would 
#  like to replace, else just input an empty string.
# 1: 1
# 2: 3
# 3: ""
# Read 2 items
# You succesfully replaced 2 elements. Your set now looks as follows 
#
#  2 4 2 3 4


dice.roll这是我的函数版本,它使用递归来滚动额外的值,这样我们只有不超过2个
6s
。请注意,我将
rolls
向量放在函数外部,以便从函数内部替换第三、第四或…
6
,我们使用复杂赋值运算符
这是我的函数版本,它使用递归滚动额外的值,因此我们只有不超过2个
6s
。请注意,我将
rolls
向量放在函数外部,以便从函数内部替换第三个、第四个或…
6
,我们还使用了复数赋值运算符
呃,我有个问题,不妨在这里问一下,我如何从样本中删除值?那么,假设我想让用户删除一个卷,然后从样本中删除他们说的值,我该怎么做?第二个问题取决于用户如何指定要删除的卷,他会说卷在向量上的位置吗(“删除第三行”),或者其他什么?假设我们在我的第一个问题中使用了卷,我们问用户“您想删除哪个卷?”?"让我们假设他们选择3。然后我如何删除该3值或他们选择的任何值?因此,用户将针对特定值而不是该值的位置,但是,假设他们选择3,我可以使用一个基于其位置删除该值的代码。我要说的是,如果他们选择3,例如t移除样本中的第二个值(其中3为)对我来说也没问题,唯一的问题是你有
replace=TRUE
你可能有多个3值,在这种情况下你想删除所有的3吗?另外,我还有一个问题,不妨在这里问一下,我如何从样本中删除值?那么,假设我想让用户删除一个卷,然后删除他们从样本中得出的值,我将如何做到这一点?第二个问题取决于用户如何指定要删除的卷,他是说卷在向量上的位置(“删除第三行”)还是其他什么?因此,假设我们在第一个问题中使用卷,我们问用户“您要删除哪个卷?”让我们假设他们选择3。然后我如何删除该3值或他们选择的任何值?因此,用户将针对特定值而不是该值的位置,但是,假设他们选择3,我可以使用一个基于其位置删除该值的代码。我要说的是,如果他们选择3,例如t移除样本中的第二个值(其中3为)对我来说也没问题,唯一的问题是你有
replace=TRUE
你可能有一个以上的3值,在这种情况下你想删除所有的3值吗?哇,这很完美,谢谢很多哇,这很完美,谢谢很多Hanks,这非常有效。简单一个问题,你对我第二个问题的回答每分钟都有效但是,我是否也可以要求用户输入一个要删除的值,将其存储为一个对象(我们称之为“Player_remove”),然后设置“remove=Player_remove”,这是可能的,还是“remove”必须设置为一个数值?请检查我的编辑:)好的,所以在我的代码中我使用了一个提示,我设置了“first_number”,这样我就可以打印(Rolls=Rolls)了[-which(Rolls==first_number)[1]]?非常感谢,这非常有效。只是一个简单的问题,您对我的第二个问题的回答非常有效,但是,我是否也可以要求用户删除一个值,将其存储为一个对象(我们称之为“Player”)_