I am stuck with a sum that I don't know how to solve this problem. I need to sum even numbers from 2 to 20, and this numbers sum 3.
n<-20
j<-0
for (i in 1:n) {
if(i %% 2 == 0)
j<-i+3
print(j)
}
Output:
[1] 0
[1] 5
[1] 5
[1] 7
[1] 7
[1] 9
[1] 9
[1] 11
[1] 11
[1] 13
[1] 13
[1] 15
[1] 15
[1] 17
[1] 17
[1] 19
[1] 19
[1] 21
[1] 21
[1] 23
With the function that I used I got the answer, but I don't know why it is repeated twice.
Add curly braces:
for (i in 1:n) {
if(i %% 2 == 0){
j <- i+3
print(j)
}
}
You can get the same outcome without using the loop:
vec <- 1:20
vec[vec %% 2 == 0] + 3
Related
I am using this code that generates 3 random numbers that add to 72:
# https://stackoverflow.com/questions/24845909/generate-n-random-integers-that-sum-to-m-in-r
rand_vect <- function(N, M, sd = 1, pos.only = TRUE) {
vec <- rnorm(N, M/N, sd)
if (abs(sum(vec)) < 0.01) vec <- vec + 1
vec <- round(vec / sum(vec) * M)
deviation <- M - sum(vec)
for (. in seq_len(abs(deviation))) {
vec[i] <- vec[i <- sample(N, 1)] + sign(deviation)
}
if (pos.only) while (any(vec < 0)) {
negs <- vec < 0
pos <- vec > 0
vec[negs][i] <- vec[negs][i <- sample(sum(negs), 1)] + 1
vec[pos][i] <- vec[pos ][i <- sample(sum(pos ), 1)] - 1
}
vec
}
If I run this code 100 times:
results <- list()
for (i in 1:100)
{
r_i = rand_vect(3,72)
results[[i]] <- r_i
}
When I look at the numbers that came out:
[[1]]
[1] 23 24 25
[[2]]
[1] 25 24 23
[[3]]
[1] 24 25 23
[[4]]
[1] 23 24 25
[[5]]
[1] 25 24 23
[[6]]
[1] 24 24 24
[[7]]
[1] 24 25 23
[[8]]
[1] 24 25 23
[[9]]
[1] 24 25 23
[[10]]
[1] 24 23 25
In each iteration, all the numbers add to 72 as expected - but the numbers don't really "look that random". They all seem to be "clumped" around "24, 23, 25". I was hoping to see more "randomness" in these numbers. For example:
[[11]]
[1] 5 50 17
[[12]]
[1] 12 40 20
Why are the numbers in the code I am using "clumped" around 24, 23, 25 - and how can I change the above code so that there is more "randomness" in the numbers being generated?
Thank you!
If you just want three random integers that sum to 72 you could do something like
diff(c(0, sort(sample(72, 2)), 72))
Let's assume I have this vector v:
v = seq(1,30,1)
I write this simple loop:
for(i in v) {
print(i)
}
However, I would like to write a loop that gives me, in time, 1:2, 3:4, 5:6, 7:8, etc. I would then get:
[1] 1,2
[1] 3,4
[1] 5,6
[1] 7,8
...
Can anyone help me?
Thanks!
Maybe you can generate v with step of 2.
v = seq(1,30,2)
for(i in v) {
cat(paste(i, i + 1, sep = ','), '\n')
}
#1,2
#3,4
#5,6
#7,8
#9,10
#11,12
#13,14
#...
If you want to keep your approach, try this:
for(i in v[-length(v)]) {
print(c(i, i+1))
}
[1] 1 2
[1] 2 3
[1] 3 4
...
Adding i to a subset.
for(i in v) {
print(v[0:1 + i])
}
# [1] 1 2
# [1] 2 3
# [1] 3 4
# [1] ...
Alternatively you could also consider this:
cbind(v[-length(v)], v[-1])
# [,1] [,2]
# [1,] 1 2
# [2,] 2 3
# [3,] 3 4
# [4,] 4 5
# [5,] 5 6
# [6,] ...
You need to update the print command and use the method range
https://www.w3schools.com/python/ref_func_range.asp
for i in range(0,len(v)-1,2):
print(str(v[i])+","+str(v[i+1]))
In this way you should get
1,2
3,4
5,6
7,8
...
Try this
> for(i in v) if (i%%2) print(c(i,i+1))
[1] 1 2
[1] 3 4
[1] 5 6
[1] 7 8
[1] 9 10
[1] 11 12
[1] 13 14
[1] 15 16
[1] 17 18
[1] 19 20
[1] 21 22
[1] 23 24
[1] 25 26
[1] 27 28
[1] 29 30
New R learner here. I have a list of odd numbers that I produced in R. I have 1. . .23 odd. "Three" is supposed to be spelled out. I only need the rows with the values of 1-11 odd not 13-23 odd. I am unable to reduce the list to be 1-11. How can I get rid of the values that are not needed?
for (i in 0:11)
{
i<-(i*2+1)
{
if (i <= 11)
{
if (i == 3) ("three")
}
}
print(i)
}
[1] 1
[1] 3
[1] 5
[1] 7
[1] 9
[1] 11
[1] 13
[1] 15
[1] 17
[1] 19
[1] 21
[1] 23
Re-arranging and tweaking your code
for (i in 0:11){
i <- (i*2+1)
if(i==3){
print("Three")
next
}
if(i<=11){
print(i)
}
}
#Output
# [1] 1
# [1] "Three"
# [1] 5
# [1] 7
# [1] 9
# [1] 11
Base R one liner:
ifelse(c(1:11)[1:11 %% 2 > 0] == 3, "three", c(1:11)[1:11 %% 2 > 0])
Maintaining flow control in loop (as requested):
x <- vector("character", sum(0:11 %% 2 >0))
seq_vals <- 1:11
odd_vals <- seq_vals[1:11 %% 2 > 0]
for(i in odd_vals){
if(i != 3){
x <- c(x, as.character(i))
}else{
x <- c(x, "three")
}
}
x[x != ""]
I've scanned quite some fora on the internet but couldn't find a clear answer to my problem, hence I decided to post it here. The program I am using is R.
The following problem is where I can't seem to find a solution. I am tasked with constructing a vector (1,2,2,3,3,3...,10,...,10) using a nested loop (so no rep()). So far I managed to construct a list of all elements but can't manage to convert it into the desired vector. I have tried quite some methods, like converting the data into a matrix and transposing it etc.
So far not a single method has worked, perhaps someone with more insight on this matter could help me.
This is what I've got so far:
for (i in 1:10){
for (j in 1:10)
if (j<=i)
{
x = c(i)
print(x)
}
}
which provides me with:
[1] 1
[1] 2
[1] 2
[1] 3
[1] 3
[1] 3
[1] 4
[1] 4
[1] 4
[1] 4
[1] 5
[1] 5
[1] 5
[1] 5
[1] 5
[1] 6
[1] 6
[1] 6
[1] 6
[1] 6
[1] 6
[1] 7
[1] 7
[1] 7
[1] 7
[1] 7
[1] 7
[1] 7
[1] 8
[1] 8
[1] 8
[1] 8
[1] 8
[1] 8
[1] 8
[1] 8
[1] 9
[1] 9
[1] 9
[1] 9
[1] 9
[1] 9
[1] 9
[1] 9
[1] 9
[1] 10
[1] 10
[1] 10
[1] 10
[1] 10
[1] 10
[1] 10
[1] 10
[1] 10
[1] 10
Thanks in advance!
If storing the result in a vector, instead of printing it, is the remaining problem, this can be done this way:
result <- vector(mode = "integer")
k <- 1
for (i in 1:10){
for (j in 1:10)
if (j<=i)
{
result[k] = c(i)
k <- k+1
}
}
head(result)
What is the meaning of for(t in 2:1000)?
x = e = rnorm(1000)
for (t in 2:1000) x[t] = x[t-1] + e[t]
windows()
plot(x, type="l")
acf(x)
acf(diff(x))
Try this to see:
for (t in 2:20) { x[t] = x[t-1] + e[t]
print(t)}
[1] 2
[1] 3
[1] 4
[1] 5
[1] 6
[1] 7
[1] 8
[1] 9
[1] 10
[1] 11
[1] 12
[1] 13
[1] 14
[1] 15
[1] 16
[1] 17
[1] 18
[1] 19
[1] 20