is there any R function for finding the index of an element in a vector? - vector

In R, I have an element x and a vector v. I want to find the first index of an element in v that is equal to x. I know that one way to do this is: which(x == v)[[1]], but that seems excessively inefficient. Is there a more direct way to do it?
For bonus points, is there a function that works if x is a vector? That is, it should return a vector of indices indicating the position of each element of x in v.
its not an difficult task

Related

How to apply function to matrix elements based on row and column identity in R

I have a square matrix, and I'm trying to:
Change only the off-diagonal elements
Apply a function to each off-diagonal element based on the identity of the element's row and column, using a separate list of values of length row/column.
I want to multiply each element of the matrix by row number element of the list, and olumn number element of the list.
Here's the matrix and the list from which I want my function to draw values to multiply for each matrix element.
states <- matrix(seq(1,9,1), nrow=3,ncol=3)
print(states)
rates <- seq(1, 5,2)
print(rates)
So for example, I want to multiply 8 in the matrix by 3*5 since it is in the 2nd row and 3rd column.
I've indexed the off-diagonal elements (not sure if this was the best way for my purposes):
delta <- row(states)-col(states)
And tried to sapply:
sapply(states[delta>0],function(i) {
i*rates[row(i)]*rates[col(i)]
print(states)
})
But I get the error message " Error in row(i) : a matrix-like object is required as argument to 'row' ". I know it's a problem with how I'm indexing the elements but can't think of a better way.
Thanks in advance!
Figured out the answer!
The problem arose from trying to pick out a single value in the matrix, which doesn't work for functions row() and col().
So rather than indexing as
rates[row(i)]
it needed to be
rates[row(states)[i]]

Creating vector with minimum values of another vector [R]

I have a problem that is very simple, but curiously it is taking me longer than I imagined. I'll put it generically here, and in the end I put an example. I hope to be clear:
I have a numeric vector X1, with N observations. I need to create another vector, X2, based based on minimum value of vector X1.
But the condition is a little strange: every observation of this vector X2 must be the minimum value of the last 126 observations of vector X1, in the same position.
In other words, each observation i of vector X2 is the result of the minimum between (i-125):i of vector X1.
I have tried in many ways, by for, creating functions and using apply, using index, but it did not work.
Consider the example:
set.seed(1)
x1<-rnorm(500,2,3)
i<-seq(126,length(x1))
x2<-min(x1[(i-125):i])
and the warning msgs:
Warning messages:
1: In (i - 125):i :
numerical expression has 375 elements: only the first used
or
for(j in 126:length(x1)){
x2<-rep(NA,length(x1))
x2[j]<-(min(x1[j-125:j]))
}
In this case, only the last observation (500) which does not result in NA.
The for loop should have had the same parentheses you had in the attempt that prompted the warnings (and should NOT have initialized the vector inside the for-loop, since that erased the prior values.):
x2<-rep(NA,length(x1))
for(j in 126:length(x1)){
x2[j]<-(min(x1[(j-125):j]))
}

Find the index of the range R

I am trying to build a function that given a vector of numbers and a vector limits it finds the range that the elements of the first vector belongs.
If i have a vector that looks like this one
numbers<-c(3,10,4,6,1)
and a vector of limits such as
limits<-c(2,5,7,12)
the result would be
1,3,1,2,NA
I have tried with which or match but no much luck so far.

For loop in R - creating new vector

I have another problem in R. I need to combine four vectors into one vector. Imagine four vectors of the same length, and I wish to combine like: first element of vector a multiply by first element of vector b plus first element of vector c multiply by first element of vector d.
Here is what I have tried:
x<-rep(5,5)
a<-seq(1,5,1)
c<-rep(1,5)
d<-rep(2,5)
div<-NULL
for(i in 1:5){
div[i]<-x[i]*a[i]+c[i]*d[i]
div<-rbind(div[i])
}
div
[,1]
[1,] 27
I really think that the outcome of this loop should be a vector, but my outcome is just a number. What am I doing wrong?
Yes, div<-rbind(div[i]) is wrong, should not have been there at all because it overwrites all your previously computed data. If you remove it, the result will probably be correct, but you can instead just perform a vectorized operation and not need a loop, like this:
div <- a * x + c * d
This will perform the computation on each set of values of those 4 vectors; the result will a new vector with what you wanted to accomplish as a result.

Choosing highest number in vector and knowing its position in R

I have a vector in R:
y=c(-29.900900, 5.728916, 35.234331, 11.854811, 61.309519, 50.432798, -27.654741, 21.413622, -10.805339, -37.504199)
I would like to select the highest number using for example max(y).
Then it gives me 61.309519.
BUT in addition I would like to know the position i.e. that its the 5th element in the vector.
Is there any way to do this? I'm ok with using other data formats eg. matrix or data.frame
which.max()
function will return the index of first maximum element. This might be important when you have more than one maximum in vector.
Since you mentioned using matrices,
which(mymatrix == max(mymatrix), arr.ind=TRUE) will locate the element by row and column.

Resources