Sequences in R with changing increments - r

How would I create an R sequence that returns
1, 4, 9, 16, 25, . . . , 100
I have tried
seq(from=1,to=100,by=(seq(from=3,to=100,by=2)))
but this gives me the completely wrong answer
Also, to get:
1, −2, 3, 4, −5, 6, 7, −8, . . . , −98, 99, 100
would I use a true false vector?

Try
v <- (1:10)^2
# [1] 1 4 9 16 25 36 49 64 81 100
All of the mathematical operations are vectorized in R, so just square the vector and you're good to go.

For the second sequence the rep function will be helpful. The code
rep(c(1,-1,1),33)*(1:99)
generates the required sequence from 1 to 99.

Related

Create new vector with all elements between elements of two equal length vectors

I am cleaning out interjections and heckles from the minutes of parliamentary sessions. I have transformed the PDF into a vector where each element contains one line of the minutes.
Using regular expressions, I have identified the indices of the lines where an interjection starts (it begins with "(") and where it ends (it ends with ")"), resulting in two equal length vectors.
To figure out now which lines to drop, I need to create a new vector that contains both the starting points and the end points, as well as all the lines in between.
As an example:
start <- c(1, 6, 9, 24)
end <- c(3, 7, 12, 27)
The resulting vector in this case should be equivalent to:
interjections <- c(1,2,3,6,7,9,10,11,12,24,25,26,27)
or alternatively:
interjection <- c(1:3, 6:7, 9:12, 24:27)
I am sure there is a simple way to do this, but I just cannot get it to work properly. Can someone help me out? Thanks!
Using Map or mapply should work for equal length vectors. If they are not equal length it will start to recycle the shorter one.
start <- c(1, 6, 9, 24)
end <- c(3, 7, 12, 27)
interjection <- Map(`:`, start, end)
interjection
[[1]]
[1] 1 2 3
[[2]]
[1] 6 7
[[3]]
[1] 9 10 11 12
[[4]]
[1] 24 25 26 27
interjections <- unlist(interjection)
interjections
[1] 1 2 3 6 7 9 10 11 12 24 25 26 27

calculating distance between values in vector R

I have the following multiset X, in which I want to find the distances between all the numbers. Is there any way to integrate this into a FOR LOOP so that If I was given a different sized multiset, I wouldn't have to manually do it like i did below?
the final answer IS [0,2, 2, 3, 3, 4, 5, 6, 7, 8, 10] (sorted) for this example
X=c(0,10,8,3,6)
L=length(X)
print(L)
##for(i in seq(from=1, to=L )){}
print(abs(X[1]-X[2]), abs(X[1]-X[3]),
abs(X[1]-X[4]), abs(X[1]-X[5]),
abs(X[1]-X[6]),
abs(X[2]-X[3]), abs(X[2]-X[4]),
abs(X[2]-X[5]), abs(X[2]-X[6]),
abs(X[3]-X[4]), abs(X[3]-X[5]),
abs(X[3]-X[6]),
abs(X[4]-X[5]), abs(X[4]-X[6]),
abs(X[5]-X[6])
)
You may see this vector as a column vector and apply dist:
sort(dist(X))
# [1] 2 2 3 3 4 5 6 7 8 10

Sort, by the difference of two numbers

I want to sort an array in increasing way, by the difference of the biggest and smallest number.
Without loops.
I think I need a sort that I can give a condition but i can't find how.
Something like this:
sort(arr, decreasing = FALSE, by = max(a) - min(a))
sort(arr, decreasing = FALSE, condition = max(a) - min(a))
The sorted array have to look like this. The difference from the first and second number is the smallest for all numbers int the array, the difference from the second and the third is the second smallest ......
Example: // I thing is like this
array(22, 2, 32, 3, 6, 9, 7, 23, 11, 13)
sorted_array(9, 11, 7, 13, 6, 22, 3, 23, 2, 32)
I thing another way is to construct the sorted array be putting on the last position the biggest number after that the smallest, the second biggest, the second smallest, ...
Sorry for the bad explanation.
This is a idea how it could work, but only for arrays where the length is even. If you want to use this solution and you have uneven arrays, you can work with if. I need to admit that it have to be urgent, that I would like to use a construction like this instead of a loop.
x <- c(22, 2, 32, 3, 6, 9, 7, 23, 11, 13)
n <- length(x)
m <- floor(n/2)
rev(
as.numeric(
rbind(
sort(x)[n-c(0:(m-1))],
sort(x)[1:m]
)
)
)
I attempted to come up with a non-for-loop construction. So I first sorted the sequence and then split it in two halves by naming the first half "a_N" and second half "b_N", then "folded it " into a two column matrix with the first half reversed, and finally read it out by unfolding with c:
my_arr <- c(22, 2, 32, 3, 6, 9, 7, 23, 11, 13)
names(my_arr) <- paste0( rep( c("a","b"), each=length(my_arr)/2), order(my_arr) )
c( rbind( sort( my_arr[grep("a", names(my_arr))], decreasing=TRUE), #first half
my_arr[grep("b", names(my_arr))]) ) # second half
#[1] 9 11 7 13 6 22 3 23 2 32
You can see the intermediate value of the matrix:
rbind( sort( my_arr[grep("a", names(my_arr))], decreasing=TRUE), my_arr[grep("b", names(my_arr))])
a5 a4 a3 a2 a1
[1,] 9 7 6 3 2
[2,] 11 13 22 23 32
And since R matrices are read out in column order you get the desired interleaving with c() which also removes the names.

Finding an associated function among numbers

We have a set of 3 numbers and a number which is function output of these numbers.
For example:
2, 4, 9 = 88
3, 7, 8 = 76
4, 9, 6 = 55
Now the function is for example
( last digit X 10 ) - (second digit - first digit)
Thus
5, 8, 2 = 17
But we have to find out this formula by trial and error method.
Is there any mathematical technique, tool or any program to find out the function associated among the numbers?

How to preserve the order of a vector in a table in R?

Pretty simple question, I assume. I am trying to do this for a different type of object (with class 'acf' and type 'list'), but I assume the answer is easily extendable for a vector (class numeric, type 'double'):
x<-c(4, 5, 6, 1, 2, 10, 15)
table(x)
x
1 2 4 5 6 10 15
1 1 1 1 1 1 1
I would like the output of the table to be in the same order as the vector (4, 5, 6, 1, 2, 10, 15). How can I achieve this?
table(factor(x, levels=unique(x)))

Resources