Reputation: 89
If I have a sample of numbers, say a set of dice rolls given by:
Rolls <- sample(1:6, 4, replace = TRUE)
What I would like to do is take any two values in the sample set and be able to add them, however, after adding them I'd like to put the new value into the sample AND remove the values that were used in the addition.
For example, say my sample is 2 4 3 6 and I choose to add 4 and 6, I would then want my sample to look like this: 2 3 10 (the order they are arranged in isn't important for me)
(Side note: It would also be great if the code would then ask the user if they'd like to add the 2 leftover values or not, in the case I've given, after adding 10 to the sample and removing 4 and 6, it would then ask "would you like to add your other two values?" and if the user says yes it would add 2 and 3, put 5 in the sample then remove 2 and 3, same as for 4 and 6.)
Any help would be much appreciated
Upvotes: 0
Views: 360
Reputation: 573
Here is a simple solution you could use.
# Create our Array of four
Rolls<-sample(1:6, 4, replace = TRUE)
cat("Values from our sample are: ", Rolls)
# Auxiliary variable to check if the input is correct
selectedVariables <- FALSE
values <- vector()
# Get the selected variables by the user
while(!selectedVariables){
number <- readline(prompt='Write two values from the sample (separate them using a comma): ')
# Transform input to int array
values <- as.integer(unlist(strsplit(number,",")))
# Check if both values are in the array considering repetitions
# (if the user select twice the same number and there is only one in the sample reject the input given)
if(values[1] %in% Rolls & values[2] %in% Rolls[-match(values[1],Rolls) & length(values)==2]){
selectedVariables = TRUE
}
else{
cat("Please select two valid values from the sample. Example: ", sample(Rolls,2))
}
}
# Depending on whether or not they are equal, we will use "which" or "match"
if (values[1] == values[2]){
# If they are equal we'll use which to get both positions of this values
RollsAux<- append(Rolls[! 1:length(Rolls) %in% which(Rolls %in% values)], sum(values))
}else{
# If they are different we'll get only the first appearance in our array of four
RollsAux<- append(Rolls[! 1:length(Rolls) %in% match(values,Rolls)], sum(values))
}
ans <- readline(prompt="Would you like to add up the other numbers? [y/n] ")
if (ans == "y" || ans == "Y"){
# If the user wants to sum the other two values this adds up the first two values in a new array with the last result
Result<- append(RollsAux[length(RollsAux)], sum(RollsAux[1:length(RollsAux)-1]))
}else{
# If not, the resulting array will be the same as our auxiliary array
Result <- RollsAux
}
Like I've mentioned in the comments you could also force the user to insert the positions of the values that he wants to select like this:
while(!selectedVariables){
number <- readline(prompt='Write the positions of the two values from the sample (separate them using a comma): ')
# Transform input to int array
values <- as.integer(unlist(strsplit(number,",")))
# Check if the user has given the same position twice
if (any(duplicated(values))){
print("Positions need to be different.")
}
else{
# Checks if all the positions given are in the length of the array
if(all(values <= length(Rolls))&length(values)==2){
values <- Rolls[values]
selectedVariables = TRUE
}
else{
print("Positions selected exceed length of array sample.")
}
}
}
Upvotes: 1
Reputation: 1816
As in your previous question we can utilize scan()
to handle user input.
Code
sum.dice <- function(Roll, p1, p2){
# extract values
tmp1 <- Roll[c(p1, p2)]
# ad and remove
tmp2 <- c(Roll[-c(p1, p2)], sum(tmp1))
# User input whether to add remaining wo numbers
cat("Your sample currently looks as follows: \n\n",
tmp2
,"\n\n Would you like to sum up the remaining two entries of your sample?")
# scan
tmp3 <- scan(what = "")
# add remaining values if so desired
if(tolower(tmp3) == "yes"){
c(sum(Roll[-c(p1, p2)]), sum(tmp1))
} else{
tmp2
}
}
sum.dice(Rolls, 3, 4)
# Your sample currently looks as follows:
#
# 5 3 6
#
# Would you like to sum up the remaining two entries of your sample?
# 1: no
# 2: ""
# Read 1 item
# [1] 5 3 6
# or
# Your sample currently looks as follows:
#
# 5 3 6
#
# Would you like to sum up the remaining two entries of your sample?
# 1: yes
# 2: ""
# Read 1 item
# [1] 8 6
Upvotes: 0
Reputation: 285
I suggest you create a smaller generic function like so, which will change your sample by adding together two chosen values of the sample:
change_sample <- function(sample, i1, i2) {
new_sample <- sample
new_sample[i2] <- sample[i1] + sample[i2]
return(new_sample[-i1])
}
If you then want to apply this multiple times, you could chain each application of this function together to achieve your goal.
Upvotes: 0