I've been having a problem in
sum <- (abs(x[i, i] - x[i, j])^p) ^ (1/p)
I want it to be for example x[1,1] ,x[2,2] etc. So I thought x[i,i] in the for loop would do the job, but it's only giving me back 0 as a result.
example <- function (x,p) {
sum <- 0
for (i in 1:ncol(x)) {
for (j in i:nrow(x)){
sum<-(abs(x[i,i] - x[i,j])^p) ^ (1/p)
}
}
return (sum)
}
#x is a matrix
You are replacing sum each time you loop through,
sum <- sum + (abs(x[i,i] - x[i,j])^p) ^ (1/p) should do.
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
I have a vector named y which has n numerical elements and a matrix n*n named w where its elements are numbers. I want to use the below formula for the above data:
I have written the following code in R using functions colSums and sum:
dy<-y-mean(y)
n*(y-mean(y))*colSums(w*dy)/sum(dy^2)#=local[,1]
Now, I want to change my above code to write it with for loop (without using colSums and sum). Indeed, I want to make the formula using for loop instead of using colSums and sum.
Thank you in advance for your help.
I don't know why you want to use a for-loop since in this case there are many disadvantages, but here we go.
First we calculate the donominator:
dy_square_sum <- 0
for (i in seq_along(y)) {
dy_square_sum <- dy_square_sum + dy[i]^2
}
In the next step, we calculate the enumerator and build up your desired output:
weighted_sum <- rep(0,n)
output <- rep(0, n)
for (i in seq_along(y)) {
for (j in seq_along(y)) {
weighted_sum[i] <- weighted_sum[i] + w[j,i] * dy[j]
}
output[i] <- n * dy[i] / dy_square_sum * weighted_sum[i]
}
or a little simplified but less efficient
output <- rep(0, n)
for (i in seq_along(y)) {
for (j in seq_along(y)) {
output[i] <- output[i] + n * dy[i] * w[j,i] * dy[j] / dy_square_sum
}
}
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.
I have an r code where I am printing the values and doing a small calculation on it.
for (j in sample.data1$SENTIMENT_STRENGTH_91D) {
i = (j - max(sample.data1$SENTIMENT_STRENGTH_91D))/C
}
this prints out each value of i. How can I put these values into a vector inside the loop?
I have tried this code but it only gives the last values
for (j in sample.data1$SENTIMENT_STRENGTH_91D) {
i = (j - max(sample.data1$SENTIMENT_STRENGTH_91D))/C
D <- c(i)
}
You can change your for loop to the following:
D = numeric(0)
for (j in sample.data1$SENTIMENT_STRENGTH_91D) {
i = (j - max(sample.data1$SENTIMENT_STRENGTH_91D))/C
D <- c(D, i)
}
But it turns out growing an object in a for loop in R is very slow. You probably don't even need a for loop. You can try taking advantage of R's vectorization:
x <- sample.data1$SENTIMENT_STRENGTH_91D
D <- (x - max(x))/C
I am new to writing loop functions and I am trying to solve this. I would like the y matrix to be populated with the values obtained from the for loop. Unfortunately y remains blank and full of 0's after the loop is executed.
mv <- c(0,1,2) # location vector
s <- 1 # scale
increment <- seq(-6,6,0.01) # Create a sequence of x values
y=matrix(0,length(increment),length(mv))
for (i in length(increment)) {
for (j in length(mv)) {
y[i,j] <- 1/(1+ exp(-(increment[i]-mv[j])/s))
}
}
Change your loop to start at 1, for now it is only using 1 value (length(increment)):
for (i in 1:length(increment)) {
for (j in 1:length(mv)) {
y[i,j] <- 1/(1+ exp(-(increment[i]-mv[j])/s))
}
}