Creating a sequence with positive and negative numbers in R

I am trying to create a function for the sequence of the following rows:

1, -2, -3, 4, 5, 6, -7, -8, -9, -10 ........ n (1 positive, 2 negative, 3 positive, 4 negative ... and extends to n).

Creating a non-negative sequence is pretty simple, but these negative terms test me.

If anyone can help me with this

+6
source share
6 answers

Here is a way to do it.

myfun <- function(n) { myvec <- integer(n) for (i in seq_len(n)) { curtri <- ceiling(sqrt(i*2 + 0.25) - 0.5) myvec[i] <- i * (-1)^(curtri + 1) } return(myvec) } myfun(10) [1] 1 -2 -3 4 5 6 -7 -8 -9 -10 

It uses the fact that you can find which triangular number you are with sqrt(i*2 + 0.25) - 0.5 . Applying even to non-triangular numbers, we can determine the index of the next triangular number and use it as an indicator for -1 .

Probably the best way.

+4
source

There are so many ways to do this!

For instance:

 n <- 30 a <- 1:n m <- ceiling(uniroot(function(x, N) x*(x+1)/2 - N, N=n, interval=c(0, n))$root) b <- 2*( ((rep(1:m,1:m))[1:n] %% 2 == 1) - 0.5) a*b 
+3
source
 n <- 20 k <- n m <- do.call(cbind, rep(list(c((-1)^(seq_len(k)+1))),k)) m[upper.tri(m)] <- 0 sign <- t(m)[t(m) != 0] seq_len(n) * sign[seq_len(n)] #[1] 1 -2 -3 4 5 6 -7 -8 -9 -10 11 12 13 14 15 -16 -17 -18 -19 -20 

The value of k is too costly, but I'm too tired to do the math and find the bottom line. I leave it to you.

+2
source

For a simple loop solution:

 myfn = function(n){ nn = 1:n x=1; i=0; j=1; while(TRUE){ if(x==-1) for(k in j:(j+i)) { nn[k] = x*nn[k]; } x = x*(-1) i = i+1 j = j+i if(j>n) break } nn[1:n] } > for(i in 1:20) print(myfn(i)) [1] 1 [1] 1 -2 [1] 1 -2 -3 [1] 1 -2 -3 4 [1] 1 -2 -3 4 5 [1] 1 -2 -3 4 5 6 [1] 1 -2 -3 4 5 6 -7 [1] 1 -2 -3 4 5 6 -7 -8 [1] 1 -2 -3 4 5 6 -7 -8 -9 [1] 1 -2 -3 4 5 6 -7 -8 -9 -10 [1] 1 -2 -3 4 5 6 -7 -8 -9 -10 11 [1] 1 -2 -3 4 5 6 -7 -8 -9 -10 11 12 [1] 1 -2 -3 4 5 6 -7 -8 -9 -10 11 12 13 [1] 1 -2 -3 4 5 6 -7 -8 -9 -10 11 12 13 14 [1] 1 -2 -3 4 5 6 -7 -8 -9 -10 11 12 13 14 15 [1] 1 -2 -3 4 5 6 -7 -8 -9 -10 11 12 13 14 15 -16 [1] 1 -2 -3 4 5 6 -7 -8 -9 -10 11 12 13 14 15 -16 -17 [1] 1 -2 -3 4 5 6 -7 -8 -9 -10 11 12 13 14 15 -16 -17 -18 [1] 1 -2 -3 4 5 6 -7 -8 -9 -10 11 12 13 14 15 -16 -17 -18 -19 [1] 1 -2 -3 4 5 6 -7 -8 -9 -10 11 12 13 14 15 -16 -17 -18 -19 -20 
+2
source

Although perhaps not the most elegant, but I believe that this will provide what you want.

 pos_neg_seq <- function(n){ s= seq((n*(n+1)/2)) loc <-1 for(i in 1:n){ if(i %% 2 == 0){ s[loc:(loc+i-1)] <- sapply(s[loc:(loc+i-1)], FUN = function(x) -x) } loc <- loc + i } return(s) } pos_neg_seq(4) [1] 1 -2 -3 4 5 6 -7 -8 -9 -10 

Another possible way to vector a specific length using the Vincent equation.

 pos_neg_seq <- function(n){ nn <- seq(n) m = ceiling(uniroot(function(x, N) x*(x+1)/2 - N, N=n, interval=c(0, n))$root) vec <- 1 for(i in 2:m){ vec <- append(vec, ifelse(rep(i%%2==0, i), rep(-1, i), rep(1, i))) } return(nn*vec[1:n]) } pos_neg_seq(7) [1] 1 -2 -3 4 5 6 -7 
+1
source

I can’t even say which is better, so the challenge of time will follow. Here's mine:

 pmfoo<-10 curtri <- ceiling(sqrt(pmfoo*2 + 0.25) - 0.5) pmbar<-integer() for(j in 1:(curtri)) pmbar<-c(pmbar,rep( (-1)^(j-1),j)) pmbar*1:pmfoo [1] 1 -2 -3 4 5 6 -7 -8 -9 -10 

Here are the time tests for the "best view" functions (biased view :-)):

 Rgames> x <-1e5 Rgames> microbenchmark(cgw(x),mso(x),willb(x),times=5) Unit: milliseconds expr min lq median uq max cgw(x) 46.61292 47.50237 48.40807 48.42774 52.02789 mso(x) 88.63360 97.72099 97.84286 99.00899 101.57643 willb(x) 281.88658 285.76896 286.92397 290.83628 294.96882 neval 5 5 5 

I brought Roland out because he is a major action movie in memory: - (

Run again with the modified mso code:

  microbenchmark(cgw(x),mso(x),willb(x),newmso(x),times=5) Unit: milliseconds expr min lq median uq max cgw(x) 51.25860 51.29666 56.21858 58.07190 61.32610 mso(x) 88.08966 89.17924 90.23504 93.28527 95.74666 willb(x) 280.68967 287.53589 287.81086 288.31673 292.60749 newmso(x) 71.53771 72.53193 72.68844 72.99419 79.21480 neval 5 5 5 5 
+1
source

Source: https://habr.com/ru/post/974757/


All Articles