Converting a one-dimensional array [duplicate] - r

This question already has answers here:
Select every other element from a vector
(3 answers)
Closed 8 months ago.
transform a one-dimensional array consisting of n integer elements in
such a way that the first half contains elements in odd positions, and
the second half contains elements in even positions
Source array: (1, 6, 2, 5, 4)
After process i need this: (1, 2, 4, 6, 5)

Try to use the sort() function:
Something like this:
# create a linear array
arr <- c(9, 8, 7, 6, 5, 4, 3, 2, 1)
# use of sort function to sort array
# by default it is sorted in increasing order
sort(arr)
# Output: [1] 1 2 3 4 5 6 7 8 9
And take a look at this tutorial.

Related

Multidimensional selection in a 2D array

picture of question posted in this link
Given a 2-d array arr of size n x m, a selection is defined as an array of integers such that it contains at least [m/2] integers from each row of arr. The cost of the selection is defined as the maximum difference between any two integers of the selection.
Suppose k is the minimum cost of all the possible selections for the given 2-d array. Find the maximum value of the product of k * the number of integers considered in the selection with the minimum cost.
Example
Suppose n= 3, m = 2, and arr = [[1, 2], [3, 4], [8, 9]]
Some of the possible selections are [2, 3, 8], [1, 2, 3, 9], [1, 3, 4, 8, 9] etc. The cost of these selections are 8 - 2 = 6, 9 - 1 = 8, and 8 respectively.
Here the minimum cost of all the possible selections is 6. The possible selections with the cost 6 are [2, 4, 8] and [2, 3, 4, 8]. The maximum value of the required product is obtained using the latter selection i.e. 6 * 4 = 24. Hence the answer is 24.
How do you go about this problem? This is the basic thought process:
Find the selections (how do you do that?)
calculate the cost of each selection
determine the least cost and multiply it with the length of the selection.
Unfortunately I am stuck on the very first step which is to find the selections. How can we do that in an efficient manner? Can we use combinations on 2D arrays?
Any help would be appreciated, thank you!

In R, transform a vector with repeated values to a vector of single ordered values including the repetitions?

I have a vector x <- c(5, 5, 5, 3, 2, 2, 5, 5, 2). I need to get a new vector with the ordered distinct (but duplicates allowed) values:
y <- c(5, 3, 2, 5, 2)
I'm sure it's a trivial problem but I've been looking around and I don't see a simple way to do it.
Just out of the curiosity, I've tried to solve it without rle(x)$values:
x[x != c(x[-1], Inf)]
# [1] 5 3 2 5 2
Basically we are just comparing the original vector, and its' shifted to the left version, to create a subset index. So if the current element is equal to the next element, we return FALSE to remove this element from the initial vector.
555322552
|||||||||
55322552i
|||||||||
001101011
|||||||||
xx53x2x52
result: 53252
We always return the last element (hopefully, there are no NAs or NULLs).

Create Vector of Length n in Julia

I want to create a vector/array of length n to be filled afterwards.
How can I do that?
And does it have to be filled already with something?
For example if you want a Vector of Ints of length 10 you can write
v = Vector{Int}(undef, 10)
And more general for an Array of Ints of dimensions (2, 3, 4)
a = Array{Int}(undef, (2, 3, 4))
Note that this fills the Vector/Array with garbage values, so this can be a bit dangerous. As an alternative you can use
v = Vector{Int}()
sizehint!(v, 10)
push!(v, 1) # add a one to the end of the Vector
append!(v, (2, 3, 4, 5, 6, 7, 8, 9, 10)) # add values 2 to 9 to the end of the vector
sizehint! is not necessary, but it can improve performance, because it tells Julia to expect 10 values.
There are other functions such as zeros, ones or fill that can provide a Vector/Array with already filled in data.

multiply every number in c() using R [duplicate]

This question already has answers here:
multiplying all elements of a vector in R
(2 answers)
Closed 4 years ago.
How to use for loop to multiply every number in the given vector L = c(2,5,8,9,...n)?
Not quite sure how to multiply everything together..
L<-c(3,5,7,9)
for (n in L){
print(n)
}
Note that unlike some other languages, R makes a specific distinction between vectors and lists. Normally c(2, 5, 8, 9) as you've written produces a vector, which among other things, enables the use of prod:
L <- c(2, 5, 8, 9)
prod(L)
#> [1] 720
Created on 2018-09-21 by the reprex package (v0.2.0).

Absolute difference between a vector and a number in R

I'm new to R and I would be very grateful for an answer to my question:
I've got a vector: c(9, 11, 2, 6, 10) and the number 4 (or a vector c(4))
I want to generate a vector with the absolute difference between the first and the second one, which should look like this: c(5, 7, 2, 2, 6)
How do I do this? I can't get it to work with diff(), even after reading through the help (?diff()).
Any help is appreciated :)
x <- c(9, 11, 2, 6, 10)
abs(x - 4)
#[1] 5 7 2 2 6
abs finds the absolute value of a vector. '4' will be recycled when subtracted from x. If you have multiple values to be subtracted, they will also be recycled with a warning unless they are the same length as x.
You ran into problems with diff because it isn't designed for scalar subtraction (what you are attempting). It is better suited to finding the difference within a vector.

Resources