Extract several instances of a sequence from vector - r

Suppose I have a numeric vector vec from which I would like to extract several instances of sequence seq that are scattered all over. The sequence's starting indexes in vec are known. Ex.:
seq <- c(6, 4, 9)
vec <- c(6, 6, 4, 9, 9, 6, 4, 9, 5, 6, 6, 4, 9, 6, 4)
seq_index <- c(2, 6, 11)
What I would like to get is someting like this:
6, 6, 6
4, 4, 4
9, 9, 9
What obviously does not work is:
vec[seq_index:seq_index + length(seq) - 1]
I also played around with the apply family of functions, e.g.
lapply(X = vec, FUN = `[`, cbind(seq_index, seq_index + length(seq) - 1))
which also does not yield the expected result.
I am sure I am missing something fundamental here but cannot figure it out.
Any pointers would be highly appreciated.

Perhaps you mean this:
sapply(seq_index, function(i) vec[i:(i+length(seq)-1)])
# [,1] [,2] [,3]
#[1,] 6 6 6
#[2,] 4 4 4
#[3,] 9 9 9

Related

Is there in R function for finding index of an array?

this is going to be a body of the particular Question.
which function we are using in array .
You can find the index of the element by the functions
which() or match()
Example for using which():
# vector created
v <- c(0, 1, 2, 3, 4,
5, 6, 7, 8, 9)
# which function is used
# to get the index
which(v == 5) # output is: 6
Example for using match():
# vector created
v <- c(0, 1, 2, 3, 4,
5, 6, 7, 8, 9)
# match function is
# used to get the index
match( 5 , v ) # output is: 6
You can see here more information
For a matrix or an array, set the argument arr.ind = TRUE:
which(myarray == 5, arr.ind = TRUE)

Find index of element comparing with sorted vector

If I have a sorted vector, like
vec <- c(5, 6, 7, 8, 9, 10, 11, 12, 13, 14)
and I have
x <- 9.5
Then x is between the 5th and 6th value in my sorted row, and I want to get the index 5. How can I do it?
The following will give the result you're looking for:
x<-c(5, 6, 7, 8, 9, 10, 11, 12, 13, 14)
findInterval(9.5,x)
> [1] 5
Alternative solutions include:
> max(which(x < 9.5))
[1] 5
There should be multiple ways to do this. One way using which.max
which.max(vec > x) - 1
#[1] 5
This finds first index where vec is greater than x and then returns an index 1 less than that.
As it is sorted the opposite should work as well
which.min(vec < x) - 1
#[1] 5

New: Identifying Local Maximas using For & If Statements in R

Thanks to those who helped on my last q, I made some modifications and the code worked. However, when I increase the length of r, the max values are not being correctly stored. Can anyone identify why?
Sorry, I'm a noob.
Code:
r <- c(1, 2, 3, 4, 5, 4, 3, 4, 5, 6, 7, 5, 2, 4, 8, 11, 12, 9)
Peaks <- c()
indPeaks <- c()
n <- length(r)-1
for (x in 1:n) {
if ((x-1)<1){
if(r[x+1]<r[x]) {
Peaks <-r[x]
indPeaks <- which(r == r[x])
}
}
else{
if(r[x-1]<r[x]&r[x+1]<r[x]) {
Peaks <-r[x]
indPeaks <- which(r == r[x])
}
}
}
Returns (Environment):
indPeaks
17 # should be: 5 11 17
Peaks
12 #should be: 5 7 12
The code worked for
r <- c(1, 2, 3, 4, 5, 4, 3, 4, 5, 6, 7)
Hi and welcome to StackOverflow.
The reason why your previous code worked is because it only contained 1 peak.
You need to combine the previous value and the current value if you are using for-loops for this purpose. So in your case, when you assign the values to Peaks and indPeaks. So the code will be:
r <- c(1, 2, 3, 4, 5, 4, 3, 4, 5, 6, 7, 5, 2, 4, 8, 11, 12, 9)
Peaks <- c()
indPeaks <- c()
n <- length(r)-1
for (x in 1:n) {
if ((x-1)<1){
if(r[x+1]<r[x]) {
Peaks <-c(Peaks,r[x])
indPeaks <- c(indPeaks,x)
}
}
else{
if(r[x-1]<r[x]&r[x+1]<r[x]) {
Peaks <- c(Peaks,r[x])
indPeaks <- c(indPeaks, x)
}
}
}
With the output:
> Peaks
[1] 5 7 12
> indPeaks
[1] 5 11 17
Note that I changed the last part of your indPeaks <- c(indPeaks, x). Where the previous version was incorrect, since you had several values in r identical to r[x]. Note: I have not tried to put an effort in optimizing your code, I have just fixed so that you get the expected output. :)

How to only get value from data.frame in R?

i am trying to calculate the probabilities of 4 dices being thrown in R. I am nearly finished, i just want to know how i could possibly access ONLY the value in a specific row of my test1 dataframe? If i write rowSums(test1[1,]) it gives me both the index AND the sum, but i only want to access the sum to be able to store how many possibilities there are to get i.e. a 4 with 4 dices etc.
HereĀ“s the important place of the code.
wurf1 <- c(1, 2, 3, 4, 5, 6)
wurf2 <- c(1, 2, 3, 4, 5, 6)
wurf3 <- c(1, 2, 3, 4, 5, 6)
wurf4 <- c(1, 2, 3, 4, 5, 6)
test1 <- data.frame(expand.grid(wurf1, wurf2, wurf3, wurf4))
rowSums(test1[1,]) #this gives me:
1
4 #because the sum of the values in index 1 = 4
Thank you for your help in advance.

Linearly regress a vector against each column of a matrix

I have a very simple question which I am sure there is an elegant answer to (I am also sure the title above is inappropriate). I have a vector of y values:
y = matrix(c(1, 2, 3, 4, 5, 6, 7), nrow=7, ncol=1)
which I would like to regress against each column in a matrix, x:
x = matrix(c(1, 2, 3, 4, 5, 6, 7, 7, 6, 5, 4, 3, 2, 1, 4, 4, 4, 4, 4, 4, 4), nrow=7, ncol=3)
For example I would like to linearly regress the first column of x against y and then the second column of x against y until the last column of x is reached:
regression.1=lm(y~x[,1])
regression.2=lm(y~x[,2])
I would later like to plot the slope of these regression versus other parameters so it would be useful if the model coefficient parameters are easily accessible in the usual way:
slope.1 = summary(regression.1)$coefficients[2,1]
I am guessing a list using something like plyr but I am too new to this game to find the simplest way to code this.
store <- mapply(col.ind = 1:ncol(x),function(col.ind){ lm(y~x[,col.ind]) })
You can then access the slope using:
> store[1,]
[[1]]
(Intercept) x[, col.ind]
6.713998e-16 1.000000e+00
[[2]]
(Intercept) x[, col.ind]
8 -1
[[3]]
(Intercept) x[, col.ind]
4 NA
Another way:
regression <- apply(x, 2, function(z)lm(y~z))
slope <- sapply(regression, function(z)unname(coef(z)[2]))
Result:
> slope
[1] 1 -1 NA

Resources