I have 4 differents lines/commands (the addition is just an example)
one<- (1+1)
two<- (2+2)
three<-(3+3)
four<-(4+4)
I need to run randomly any of this four command lines (one, two, three or four), I am no focus in the addition result.
I did try with:
list=c("one", "two", "three", "four")
number <- sample(list, 1)
number
but lamentably didnt run the line/command.
I expect that the sampling can run on the console any of these 4 commands.
Thanks in advance
Seems like maybe the problem is that you aren't constructing the list correctly? Does this do what you expect?
one <- (1+1)
two <- (2+2)
three <- (3+3)
four <- (4+4)
myList <- list(one, two, three, four)
number <- sample(myList, 1)[[1]]
Because (1 + 1) evaluated to 2 and that 2 is assigned to one, it is impossible to retrieve (1 + 1) through one (if you define it the way you did). If you want to capture 1 + 1, etc, you can do the following:
one = quote(1 + 1)
two = quote(2 + 2)
three = quote(3 + 3)
ls = list(one, two, three)
x = sample(ls, 1)[[1]]
x # expression like 1 + 1
eval(x) # evaluated sum
Related
I am trying to solve the following task
Write a function that counts the number of odd and even numbers in a
vector and provides three outputs (the outputs is supposed to tell me how many odd and even numbers there are in the vector)
vector <- c(1,2,3,4,5,6,7,8,9)
Not sure what you mean by three outputs as you've listed two but this will count the number of odd and even numbers in a vector as well as give the sum.
myfunc <- function(x) {
addmargins(setNames(table(x %% 2), c("even", "odd")),1)
}
Output:
> myfunc(vector)
even odd Sum
4 5 9
Based on the feedback of the rpevious answer, here one has the solution:
> vec <- c(1,2,3,4,5,6,7) # Test Vector
> evenoddsumm <- function(vec) {
+ even <- vec[vec/2 == round(vec/2,0)]
+ odd <- vec[vec/2 != round(vec/2,0)]
+ result <- c(paste("There are ",length(even)," even numbers.", sep = ""),
+ paste("There are ",length(odd)," odd numbers.", sep = ""),
+ paste("There are ",length(odd)," odd numbers and ",length(even)," even numbers.", sep = ""))
+ return(result)
+ }
> evenoddsumm(vec)
[1] "There are 3 even numbers." "There are 4 odd numbers."
[3] "There are 4 odd numbers and 3 even numbers."
The approach is consider an even as a number that remains integer when divided by 2, which means even/2 is equal round(even/2)
I have one vector like this:
years <- c(2021:2091)
And I want to create another vector to bind to it based off an initial value and inrease compound-like for every row based on an arbitrary decimal(such as 10%, 15%, 20%):
number = x
rep(x*(1 + .10)^n, length(years))
How do I replicate the length of years for the second vector while increasing the exponent every time. Say there is 71 rows in years, I need n to start at 1 and run through 71.
I have tried:
rep(x*(1 + .10)^(1:71), length(years))
But this does it 71*71 times. I just need one value for each exponent!
Hopefully this makes sense, thanks in advance!
Here is how you could do it with a function:
future_value = function(years, x = 1, interest = 0.1) {
x * (1 + interest) ^ (1:length(years))
}
Example outputs:
> future_value(2021:2025)
[1] 1.10000 1.21000 1.33100 1.46410 1.61051
> future_value(2021:2025, x = 2, interest = 0.15)
[1] 2.300000 2.645000 3.041750 3.498012 4.022714
I am using Jenks Natural Breaks via the BAMMtools package to segment my data in RStudio Version 1.0.153. The output is a vector that shows where the natural breaks occur in my data set, as such:
[1] 14999 41689 58415 79454 110184 200746
I would like to take the output above and create the ranges inferred by the breaks. Ex: 14999-41689, 41690-58415, 58416-79454, 79455-110184, 110185-200746
Are there any functions that I can use in R Studio to accomplish this? Thank you in advance!
Input data
x <- c(14999, 41689, 58415, 79454, 110184, 200746)
If you want the ranges as characters you can do
y <- x; y[1] <- y[1] - 1 # First range given in question doesn't follow the pattern. Adjusting for that
paste(head(y, -1) + 1, tail(y, -1), sep = '-')
#[1] "14999-41689" "41690-58415" "58416-79454" "79455-110184" "110185-200746"
If you want a list of the actual sets of numbers in each range you can do
seqs <- Map(seq, head(y, -1) + 1, tail(y, -1))
You can definitely create your own function that produces the exact output you're looking for, but you can use the cut function that will give you something like this:
# example vector
x = c(14999, 41689, 58415, 79454, 110184, 200746)
# use the vector and its values as breaks
ranges = cut(x, x, dig.lab = 6)
# see the levels
levels(ranges)
#[1] "(14999,41689]" "(41689,58415]" "(58415,79454]" "(79454,110184]" "(110184,200746]"
How to use "over" on 2 lists of inputs, each time picking 1 element from the 2 lists?
E.g., there is:
(+/)[1;2 3] = +[+[1;2];3] = 6
How to do something like:
f:{x+y+z};
(f/)[1;2 3;22 33] = f[f[1;2;22];3;33] = 61
Thank you.
This should work exactly as you described, which is the fold behaviour of over /. When using it with 3 arguments the function cycles through lists of y and z applying them to the output of the previous expression. Considering the numbers you have provided:
x:1
y:2 3
z:22 33
The wiki page describes this as:
f[f[… f[f[x;y0;z0];y1;z1]; … yn-1;zn-1];yn;zn]
Which is pseudocode looks something like:
res = x + y[0] + z[0] // pass this value forward
= res + y[1] + z[1]
"over" takes two arguments each time, so three arguments is not an option: http://code.kx.com/q/ref/control/#over
To achieve what you have mentioned, the function as well as the input have to be twisted:
f:{x+y[0]+y[1]}
(f/)1,flip(2 3;22 33)
I have two vectors x and y. x is a larger vector compared to y. For example (x is set to all zeros here, but that need not be the case)
x = rep(0,20)
y = c(2,3,-1,-1)
What I want to accomplish is overlay some y's in x but at random. So in the above example, x would look like
0,0,2,3,-1,-1,0,0,0,0,2,3,-1,-1,...
Basically, I'll step through each value in x, pull a random number, and if that random number is less than some threshold, I want to overlay y for the next 4 places in x unless I've reached the end of x. Would any of the apply functions help? Thanks much in advance.
A simple way of doing it would be to choose points at random (the same length as x) from the two vectors combined:
sample(c(x, y), length(x), replace = TRUE)
If you want to introduce some probability into it, you could do something like:
p <- c(rep(2, each = length(x)), rep(1, each = length(y)))
sample(c(x, y), length(x), prob = p, replace = TRUE)
This is saying that an x point is twice as likely to be chosen over a y point (change the 2 and 1 in p accordingly for different probabilities).
Short answer: yes :-) . Write some function like
ranx <- runif(length(x)-length(y)+1)
# some loop or apply func...
if (ranx[j] < threshold) x[j:j+length(y)] <- y
# and make sure to stop the loop at length(y)-length(x)
Something like the following worked for me.
i = 1
while(i <= length(x)){
p.rand = runif(1,0,1)
if(p.rand < prob[i]){
p[i:(i+length(y))] = y
i = i+length(y)
}
i = i + 1
}
where prob[i] is some probability vector.