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

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).

Related

Converting a one-dimensional array [duplicate]

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.

understanding the output of a function

Trying to understand how the value of "traded" is 34
available <- c(10,4,7,10,12)
desired <- c(12,5,2,6,14)
traded <- sum(mapply(function(x,y) min(x,y), available, desired))
Correct value for traded is 34. Just not sure why this is the case. I thought the value would be 6 as the minimum values from each vector (4 and 2) summed together =6
This is answered in the comments, but I wanted to add this breakdown since it helps me to visualize each step.
mapply(function(x,y) min(x,y)): Maps min(x,y) to each item in vectors x and y , so the function is doing this:
min(10,12)
min(4,5)
min(7,2)
min(10,6)
min(12,14)
and outputs = (10, 4, 2, 6, 12)
sum(mapply(...)): Which "sees" the output above and computes 10+4+2+6+12 = 34

How to find number of observation between first observation and observation with maximal value

I have a large data frame and I need a function to automate this search. Basically I want to find how many observations are between the first observation and the observation with maximal value.
Example:
x <- c(2, 1, 9, 3, 4, -6, 5, 11, 6, -7, -1)
Assuming that this is my data I want to count the number of data points between 2 and 11.
I need to do this in r.
Help is highly appreciated :D !!!
We can eithe
diff(which(x %in% c(2, max(x)))) -1
#[1] 6
Or substract the index of the max value (which.max) from the first value (+1 - not including the elements)
which.max(x) - x[1]

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.

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