apply which.max to second, third, etc. highest value - r

I have a vector:
x<-rnorm(100),
I would like to create a vector that stores the position of the first, second, third...100th highest value in X.
For example if x=4,9,2,0,10,11 then the desired vector would be 6,5,2,1,3,4 is there a function for doing this?

Try using order
> order(x, decreasing =TRUE)
[1] 6 5 2 1 3 4

Try this:
> order(-x)
[1] 6 5 2 1 3 4

Related

How to create a vector of positions of a numeric vector in R?

I have a vector of numbers that contain some gaps. For example,
vec <- c(3,1,7,3,5,7)
So, there are 4 different values and I would like to transform it into a vector of values (without gaps) indicating the order of the entry while respecting the same position. So, in this case, I would like to obtain
2 1 4 2 3 4
Indicating a sequence of between 1 and 4 and showing the orders in the original vector vec.
You can use match to help you look up the values in a sorted unique order. For example
vec <- c(3,1,7,3,5,7)
match(vec, sort(unique(vec)))
# [1] 2 1 4 2 3 4
This works because match returns the indexes which will start at 1.
We may use factor
as.integer(factor(vec))
[1] 2 1 4 2 3 4

Unique with subsequent element only

My input is a vector like this
v = c(1,2,2,3,4,5,4,1,1)
unique(v) == c(1,2,3,4,5)
instead, I need to check and operate uniqueness only on pairs of subsequent element:
.f(v) == c(1,2,3,4,5,4,1)
Use rle from base R and extract the 'values'
rle(v)$values
[1] 1 2 3 4 5 4 1
unique gets the unique values from the whole dataset, whereas rle returns a list of 'values' and its lengths for each adjacent unique value
Or another option is to do a comparison with the current and adjacent value and apply duplicated to subset the vector
v[!duplicated(cumsum(c(TRUE, v[-1] != v[-length(v)])))]
[1] 1 2 3 4 5 4 1
Another possible solution:
v[v != dplyr::lag(v, default = Inf)]
#> [1] 1 2 3 4 5 4 1

Finding Index from Vector/Matrix or Dataframe in R

I have data in R as follow:
data <- c(1,12,22,0,8,1,0,0)
Is there any way to index the data to find the index for element that is greater than 0? So the result will be:
1 2 3 5 6
I tried to use as.factor(data), but it will take several more step to get the result that I aim for. Thanks.
We can use which on a logical vector
which(data >0)
#[1] 1 2 3 5 6
Another option is using seq_along (but not as straightforward as the which method by #akrun)
> seq_along(data)[data>0]
[1] 1 2 3 5 6

Find n smallest values from data?

How to get 3 minimum value on the data automatically?
Data:
data <- c(4,3,5,2,2,1,1,5,6,7,8,9)
[1] 4 3 5 2 2 1 1 5 6 7 8 9
With min() function just return 1 value and I want to get 3 minimum value from data.
min(data)
[1] 1
Can I have this from a data?
[1] 1 1 2
Simply take the first three values of a sorted vector
> sort(data)[1:3]
[1] 1 1 2
Another alternative is head function that shows you the first n values of R object, so for three highest numbers you need head of a sorted vector
> head(sort(data), 3)
[1] 1 1 2
...but you could take head of possibly any other R object.
If you were interested in value that marks the upper boundry of k percent lowest values, use quantile function
> quantile(data, 0.1)
10%
1.1
data <- c(4,3,5,2,2,1,1,5,6,7,8,9)
sort(data,decreasing=F)[1:3]

How do you convert information from rle into a data frame

I want to convert the information contained in a the "rle" function in R, into a data frame, but couldn't find how. For example, for the vector
x <- c(1,1,1,2,2,3,4,4,4)
I want a dataframe that has two columns of 1 2 3 4 and 3 2 1 3
Any help would be greatly appreciated!
Use unclass to remove the rle class. Then you can just use data.frame on the resulting list.
data.frame(unclass(rle(x)))
## lengths values
## 1 3 1
## 2 2 2
## 3 1 3
## 4 3 4
You can do it direclty with the data.frame function. rle actually returns a list of two components (lengths and values).
rleX
data.frame(values = rleX$values, lengths = rleX$lengths)
You can use this simple function to convert to dataframe
data <- with(rle(x), data.frame(values, lengths))
Try this:
data.frame(table(x))
x Freq
1 1 3
2 2 2
3 3 1
4 4 3

Resources