I have this,
sum(x^i)
with i being greater than or equal to 1.
How can I create a for loop in R for this summation?
In other words, how do I format this summation in R?
If both x and i are vectors you may use for loop as -
x <- 1:10
i <- 1:10
result <- 0
for(e in i) {
result <- result + sum(x^e)
}
result
If any of x or i goes to infinity, then the result would always be infinity.
For a fixed x and infinite n,
x <- 0.1 # You may change x
s <- 0
n <- 0
while(n < 100) { #If you want inf, let n >=0 then R will freeze recommend large number...
s <- s + x^(n)
n<-n+1
}
s
Related
I want to sum (1/3)+(3/5)+ ⋯ +(99/101) using loops in R
I have tried this and I want to check if its right and is there other solutions also if we can do it with for loops and repeat loops.
sum <- 0
i <- 1
j <- 3
while (i<=99 && j<=101 ) {
sum <- sum+i/j
i <- i+2
j <- j+2
}
print(sum)
Yes, it is correct. Although you can also do
sum <- 0
i <- 1
while (i<=99) {
sum <- sum+i/(i+2)
i <- i+2
}
sum
sum <- 0
for (i in seq(1,99,2)) {
sum <- sum+i/(i+2)
}
sum
sum <- 0
i <- 1
repeat {
sum <- sum+i/(i+2)
if (i == 99) break
i <- i + 2
}
sum
i <- seq(1,99,2)
sum(i / (i + 2))
Ah, you'd better use another name than sum for your variable.
We can use the idea of odd numbers formula where 2*n - 1 and 2*n + 1 are two consecutive odd numbers
result <- 0
for(i in 1:50){
result <- result + (2*i - 1)/(2*i + 1)
}
output
result
#>[1] 46.10465
so i'm trying to create a fibonacci sequence and I found the following syntax which works
x <- 0
y <- 1
fib <- c()
while (x < 4000000 & y < 4000000){
x <- x + y
y <- x + y
fib <- c(fib, x, y)
}
However, this syntax only create the vector as long as the value of the x and y which is used in the calculation doesn't exceed 4,000,000. is it possible to makes the while refer to "if vector fib has less than let's say 100 element"?
thanks in advance
Making a very slight change to your code, use the length() function check the length (number of elements) of fib before each iteration of the while loop.
while(length(fib)<100){....}
This way you avoid creating a further variable to count the number of iterations, as in the other answers.
For what it's worth, this is how I would do it - a for loop is more appropriate as you have a set number of iterations
n <- 100
fib <- c(0,1)
for(i in 3:n){
fib[i] <- fib[i-2] + fib[i-1]
}
Here's another way to write the code -
fib <- numeric(100)
fib[1] <- 0
fib[2] <- 1
i <- 2
while (i < 100){
i = i + 1
fib[i] = fib[i-1] + fib[i-2]
}
Growing vector in a loop (fib = c(fib, x, y)) is inefficient so I have created a vector of fixed length (100) and then assigned the value to it. This also avoid creation of unnecessary temporary variables x and y.
Just define another variable for using inside the while loop and increase it by the length of the fib,
x <- 0
y <- 1
mylen <- 0 # new variable
fib <- c()
while (mylen<100){
x <- x + y
y <- x + y
fib = c(fib, x, y)
mylen <- length(fib)
}
I have the following exercise to be solved in R. Under the exercise, there is a hint towards the solution.
Exercise: If there are no ties in the data set, the function above will produce breakpoints with h observations in the interval between two consecutive breakpoints (except the last two perhaps). If there are ties, the function will by construction return unique breakpoints, but there may be more than h observations in some intervals.
Hint:
my_breaks <-function(x, h = 5) {
x <-sort(x)
breaks <- xb <- x[1]
k <- 1
for(i in seq_along(x)[-1])
{if(k<h)
{k <- k+1}
else{
if(xb<x[i-1]&&x[i-1]<x[i])
{xb <- x[i-1]
breaks <-c(breaks, xb)
k <- 1
}
}
}
However, I am having a hard time understanding the above function particularly the following lines
for(i in seq_along(x)[-1])
{if(k<h)
{k <- k+1}
Question:
How is the for loop supposed to act in k if k is previously defined as 1 and i is different than k? How are the breakpoints chosen according to the h=5 gap if the for loop is not acting on x? Can someone explain to me how this function works?
Thanks in advance!
First, note that your example is incomplete. The return value and the final brace are missing there. Here is the correct version.
my_breaks <-function(x, h = 5) {
x <- sort(x)
breaks <- xb <- x[1]
k <- 1
for(i in seq_along(x)[-1]){
if(k<h) {
k <- k+1
} else {
if(xb<x[i-1]&&x[i-1]<x[i]){
xb <- x[i-1]
breaks <-c(breaks, xb)
k <- 1
}
}
}
breaks
}
Let's check if it works.
my_breaks(c(1,1,1:5,8:10), 2)
#[1] 1 2 4 8
my_breaks(c(1,1,1:5,8:10), 5)
#[1] 1 3
As you can see, everything is fine. And what is seq_along(x)[-1]? We could write this equation as 2:length(x). So the for loop goes through each element of the vector x in sequence, skipping the first element.
What is the k variable for? It counts the distance to take into account the h parameter.
I am trying to save the output of the code below. I know "print" is the problem, but I do not know what works instead.
I generally wonder if there is not another way instead of the for-loop: For each value in the vector (x), I want to draw a new random number (here with runif) and match it to a given value (here for example 0.5). Depending on the result, a new value for x should be stored in a vector x2 (similar to the if-else example below). Waiving the for-loop, I could not find a way to always draw a new random number for each value in vector x.
I would be very grateful for any help!
x <- c(2,2,2,3,3,3)
for(i in x){
if(runif(1) <= 0.5){
print(i + 1)
} else {
print(i)
}
}
Or you could use lapply, then you don't have to modify an object outside your loop each step.
x <- c(2,2,2,3,3,3)
x2 <- unlist(lapply(x, function(x){
if(runif(1) <= 0.5) return(x +1)
return(x)
}))
x2
Try this code:
x <- c(2,2,2,3,3,3)
x2<-NULL
for(i in 1:length(x)){
if(runif(1) <= 0.5){
x2[i]<-1
} else {
x2[i]<-2
}
}
Your output
x2
[1] 1 2 2 1 2 1
In x2 you have random numbers with given values (1 and 2) related to the runif probability.
This is the same thing in a single row:
ifelse(runif(n = length(x))<=0.5,1,2)
[1] 1 2 2 2 1 1
I would like to ask how I can compute the sum of a vector in R without using one of the the ready functions (sum, mean etc). Sorry for the silly question!!!
I tried the following but it did not work. Could you tell me what I am doing wrong?
The code is:
x<-c(1,2,3)
sumfun<-function(y){
sum<-0
for(i in 1:(length(y)-1)){
sum=sum+y[i]
}
print(sum)
}
sumfun(x)
These each return the sum of the elements in x:
Sum <- 0
for(x_ in x) Sum <- Sum + x_
Sum
Sum <- 0
for(i in seq_along(x)) Sum <- Sum + x[i]
Sum
Reduce(`+`, x)
# recursive solution
summer <- function(x) if (length(x) > 0) x[1] + Recall(x[-1]) else 0
summer(x)
sum(x)
# limited as it assumes x has three elements
x[1] + x[2] + x[3]
You should change the range of your for loop to make it read the values of the first up until the last index, thus remove the -1 in for(i in 1:(length(y)-1)). The fix makes the entire code look like:
x<-c(1,2,3)
sumfun<-function(y){
sum<-0
for(i in 1:(length(y))){
sum=sum+y[i]
}
print(sum)
}
sumfun(x)
This should print out 6.