I am trying to go through each row of a dataset and find the smallest distance formula for the select numbers in each row and put it in a vector(acc).
I have done some research with other questions and this is the code that I got.
a=1
b=1
n=1
for(i in 1:nrow(satest_tDCS_01_split)){
while(a==b){
d=100000
t=1
e=9
f=10
while(t< 5){
c<-sqrt(((satest_tDCS_01_split[n,22]-satest_tDCS_01_split[n,e])^2)
+((satest_tDCS_01_split[n,23]-satest_tDCS_01_split[n,f])^2))
if(c<d){
d<-c
}
e=as.numeric(e)+3
f=as.numeric(f)+3
t=t+1
}
acc<-c()
acc<-append(acc,d)
n=as.numeric(n)+1
a<-satest_tDCS_01[n,4]
b<-satest_tDCS_01[n+1,4]
}
}
What I am expecting is a vector(acc) of numbers d but I don't even get acc when I run it. What I think is wrong is that the for loop is wrong.
The script acc <- c() inside the loop will reset the acc in every iteration. Move it outside the loop may help.
Related
Inspired by the leetcode challenge for two sum, I wanted to solve it in R. But while trying to solve it by brute-force I run in to an issue with my for loop.
So the basic idea is that given a vector of integers, which two integers in the vector, sums up to a set target integer.
First I create 10000 integers:
set.seed(1234)
n_numbers <- 10000
nums <- sample(-10^4:10^4, n_numbers, replace = FALSE)
The I do a for loop within a for loop to check every single element against eachother.
# ensure that it is actually solvable
target <- nums[11] + nums[111]
test <- 0
for (i in 1:(length(nums)-1)) {
for (j in 1:(length(nums)-1)) {
j <- j + 1
test <- nums[i] + nums[j]
if (test == target) {
print(i)
print(j)
break
}
}
}
My problem is that it starts wildly printing numbers before ever getting to the right condition of test == target. And I cannot seem to figure out why.
I think there are several issues with your code:
First, you don't have to increase your j manually, you can do this within the for-statement. So if you really want to increase your j by 1 in every step you can just write:
for (j in 2:(length(nums)))
Second, you are breaking only the inner-loop of the for-loop. Look here Breaking out of nested loops in R for further information on that.
Third, there are several entries in nums that gave the "right" result target. Therefore, your if-condition works well and prints all combination of nums[i]+nums[j] that are equal to target.
We have to create function(K) that returns vector which has all items smaller than or equal to K from fibonacci sequence. We can assume K is fibonacci item. For example if K is 3 the function would return vector (1,1,2,3).
In general, a for loop is used when you know how many iterations you need to do, and a while loop is used when you want to keep going until a condition is met.
For this case, it sounds like you get an input K and you want to keep going until you find a Fibonacci term > K, so use a while loop.
ans <- function(n) {
x <- c(1,1)
while (length(x) <= n) {
position <- length(x)
new <- x[position] + x[position-1]
x <- c(x,new)
}
return(x[x<=n])
}
`
Tried many different loops, and this is closest I get. It works with every other number but ans(3) gives 1,1,2 even though it should give 1,1,2,3. Couldn't see what is wrong with this.
What i am trying to accomplish in the forloop and am still having difficulty with:
prb[i]=(prb[i-1]*ER)+b[i]
prb[1]=(prb[0]*ER)+b[1]
prb[2]=(prb[1]*ER)+b[2]
and then output prb[1,2,3....] from the left hand side of the equation.
Also, defining SS to reflect prb at the previous time step (i.e. (prb-1))
I have attempted to save the results from my forloop in the empty vectors. However, the values outputted into the vectors are the same values(i.e. from the first iteration) and it doesn't appear to be having the additive affect I am attempting. Somethings appears to be wrong with the logic of my code. I would like for the values from b[i] to be used in b[i+1] for the next run of the forloop. Does anyone have any ideas or solutions to problem? Best!
Matthew
#Parameters
c=0.2
A=5
d=8
d0=5
s=0.5
e=0.1
p=0.6
ER=e/A
#Colonization Equation Probabilities
C2 = c*A*exp(-d*s/d0) #ML to SS
#Empty Vectors
l=vector(mode="numeric", length=100) #open vectors to store the different probability values from the forloop
b=vector(mode="numeric", length=100)
prb=(l*ER) + b #total probability of SS being colonized
#Island States
ML=1
SS=prb
n.I=c(ML, SS)
#Forloop and Conditional Statements
for(i in 2:101) {
(SS[i]=prb[i-1])
(prb[i]=(l[i]*ER)+b[i])
if (SS < 1 ) {
(l[i]=prb[i-1])
} else if(SS < 1){
b[i]=C2
}
}
I copied your code and ran it. My observations are:
First of all, in R, vector indices start from 1 not 0.
So SS=prb[0:1] yields prb[0] which is just 0.
Since to execute the line for populating prb is conditional on SS being >= 1, you can never run this line with this condition. When prb[1] is not 1 or larger, you cannot alter it so there is a vicious circle.
Even if you can run it by satisfying the condition (a different one of course), since for the first iteration prb[0] is non existent so prb[0]*ER just yields numeric(0), which is an empty vector item!
So first you should modify your vector indices, then you should modify your condition so that it allows for a jump start.
I have written the following loop in R. I want the loop to keep running until there is a case where Y is greater than or equal to 3. I then want to display T which is the number of experiments run until this outcome occurs for the first time.
T=0
while(Y<3)
{
X=rbinom(1,5,0.5)
Y=rbinom(1,X,0.5)
Outcome=Y
T=T+1
}
T
I am very new to R and I'm unsure how to change what i've done to achieve what I need.
You can use a do until construction:
while(TRUE){
# Do things
if(Y >= 3) break()
}
You don't need a loop for this. The following uses R's vectorization and is much more efficient.
set.seed(42) #for reproducibility
n <- 1e4 #increase n and rerun if condition is never satisfied
X=rbinom(n,5,0.5)
Y=rbinom(n,X,0.5)
#has condition been satisfied?
any(Y>3)
#TRUE
#first value that satisfies the condition
which.max(Y>3)
#[1] 141
If you don't know how many times you are to perform the loop, while loop is used in that case. It performs the looping until your desired condition is satisfied. You can try the following codes.
T=0 #Index variable
Y=2 #Initial value that starts the while looping
At first while loop inspect this initial Y=2, if it satisfies the condition then the lopping starts until the condition gets dissatisfied.
while(Y<3) #Initialization of your looping
{
X=rbinom(1,5,0.5)
Y=rbinom(1,X,0.5)
T=T+1
}
T
The first time the while checks its condition it does not find Y. Try to initialize Y to something less than 3.
Y <- 0
I want to make a loop which contains two variables i,j. for each i equals 1:24, j can be 1:24
but I don't know to make this loop;
i=1
while(i<=24)
{
j=seq(1,24,by=1)
for (j in j)
{
cor[i,j]
}
}
i=i+1
is this right? my output is cor[i,j].
In order to accomplish your final goal try...
cor(myMatrix)
The result is a matrix containing all of the correlations of all of the columns in myMatrix.
If you want to try to go about it the way you were it's probably best to generate a matrix of all of the possible combinations of your items using combn. Try combn(1:4,2) and see what it looks like for a small example. For your example with 24 columns the best way to cycle through all combinations using a for loop is...
myMatrix <- matrix(rnorm(240), ncol = 24)
myIndex <- combn(1:24,2)
for(i in ncol(myIndex)){
temp <- cor(myMatrix[,myIndex[1,i]],myMatrix[,myIndex[2,i]])
print(c(myIndex[,i],temp))
}
So, it's possible to do it with a for loop in R you'd never do it that way.
(and this whole answer is based on a wild guess about what you're actually trying to accomplish because the question, and your comments, are very hard to figure out)