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
Related
I want to generate a random variable X by continuing generating U(0,1) random variables as long as their product falls below exp(-2). Then my random variable X would be equal to the number of U(0,1) random variables that was generated minus 1.
I tried using the while loop to do it but not sure why the code does not return anything in my console. Please help me point out what I did wrong?
p <- 1
counter <- 0
while(p < exp(-2)) {
u <- runif(1)
p <- p*u
counter <- counter+1
X <- counter - 1
print(X)
}
update
If you want to replicate the process by 100 times, you can use replicate
replicate(
100,
{
p <- 1
counter <- 0
repeat {
p <- p * runif(1)
counter <- counter + 1
if (p < exp(-2)) {
break
}
}
counter
}
)
I guess you could use repeat with condition p < exp(-2)
p <- 1
counter <- 0
repeat {
print(counter)
p <- p * runif(1)
counter <- counter + 1
if (p < exp(-2)) {
print(counter)
break
}
}
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
I am trying to write a function to calculate h-point. the function is defined over a rank frequency data frame.
consider the following data.frame :
DATA <-data.frame(frequency=c(49,48,46,38,29,24,23,22,15,12,12,10,10,9,9), rank=c(seq(1, 15)))
and the formula for h-point is :
if {there is an r = f(r), h-point = r }
else { h-point = f(i)j-f(j)i / j-i+f(i)-f(j) }
where f(i) and f(j) are corresponding frequencies for ith and jth ranks and i and j are adjacent ranks that i<f(i) and j>f(j).
NOW, i have tried the following codes :
fr <-function(x){d <-DATA$frequency[x]
return(d)}
for (i in 1:length(DATA$rank)) {
j <- i+1
if (i==fr(i))
return(i)
else(i<fr(i) && j>fr(j)) {
s <-fr(i)*j-fr(j)*i/j-i+fr(i)-fr(j)
return(s)
}}
I also tried:
for (i in 1:length(DATA$rank)) {
j <- i+1
if (i==fr(i))
return(i)
if (i<fr(i) while(j>fr(j))) {
s <-fr(i)*j-fr(j)*i/j-i+fr(i)-fr(j)
return(s)
}}
and neither of them works. for the DATA ,the desired result would be i=11 and j=12, so:
h-point=12×12 - 10×11 / 12 - 11 + 12 - 10
can you please tell me what I`m doing wrong here?
You could do:
h_point <- function(data){
x <- seq(nrow(data))
f_x <- data[["frequency"]][x]
h <- which(x == f_x)
if(length(h)>1) h
else{
i <- which(x<f_x)
j <- which(x>f_x)
s <- which(outer(i,j,"-") == -1, TRUE)
i <- i[s[,1]]
j <- j[s[,2]]
cat("i: ",i, "j: ", j,"\n")
f_x[i]*j - f_x[j]*i / (i-j + f_x[i]-f_x[j])
}
}
h_point(DATA)
i: 11 j: 12
[1] 34
I think I have figured out what you are trying to achieve. My loop will go through DATA and break at any point if rank == frequency for a given row. If might be more prudent to explicitly test this with DATA$rank[i] == fr(i) rather than relying on i, in case tied ranks etc.
The second if statement calculates h-point (s) for rows i and j if row i has rank that is lower than freq and row j has a rank that is higher.
Is this what you wanted?
DATA <-data.frame(frequency=c(49,48,46,38,29,24,23,22,15,12,12,10,10,9,9), rank=c(seq(1, 15)))
fr <-function(x){d <-DATA$frequency[x]
return(d)}
for(i in 1:nrow(DATA)){
j <- i+1
if (i==fr(i)){
s <- list(ij=c(i=i,j=j), h=i)
break
}else if(i <fr(i) && j>fr(j)){
s <-list(ij=c(i=i,j=j),h=fr(i)*j-fr(j)*i/j-i+fr(i)-fr(j))
}}
I am not sure the formula is correct, in your loop you had j-i but in explanation it was i-j. Not sure if the entire i-j+fr(i)-fr(j) is the denominator and similarly for the numerator. Simple fixes.
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.