New: Identifying Local Maximas using For & If Statements in R - 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. :)

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)

Extract first continuous sequence in vector

I have a vector:
as <- c(1,2,3,4,5,9)
I need to extract the first continunous sequence in the vector, starting at index 1, such that the output is the following:
1 2 3 4 5
Is there a smart function for doing this, or do I have to do something not so elegant like this:
a <- c(1,2,3,4,5,9)
is_continunous <- c()
for (i in 1:length(a)) {
if(a[i+1] - a[i] == 1) {
is_continunous <- c(is_continunous, i)
} else {
break
}
}
continunous_numbers <- c()
if(is_continunous[1] == 1) {
is_continunous <- c(is_continunous, length(is_continunous)+1)
continunous_numbers <- a[is_continunous]
}
It does the trick, but I would expect that there is a function that can already do this.
It isn't clear what you need if the index of the continuous sequence only if it starts at index one or the first sequence, whatever the beginning index is.
In both case, you need to start by checking the difference between adjacent elements:
d_as <- diff(as)
If you need the first sequence only if it starts at index 1:
if(d_as[1]==1) 1:(rle(d_as)$lengths[1]+1) else NULL
# [1] 1 2 3 4 5
rle permits to know lengths and values for each consecutive sequence of same value.
If you need the first continuous sequence, whatever the starting index is:
rle_d_as <- rle(d_as)
which(d_as==1)[1]+(0:(rle_d_as$lengths[rle_d_as$values==1][1]))
Examples (for the second option):
as <- c(1,2,3,4,5,9)
d_as <- diff(as)
rle_d_as <- rle(d_as)
which(d_as==1)[1]+(0:(rle_d_as$lengths[rle_d_as$values==1][1]))
#[1] 1 2 3 4 5
as <- c(4,3,1,2,3,4,5,9)
d_as <- diff(as)
rle_d_as <- rle(d_as)
which(d_as==1)[1]+(0:(rle_d_as$lengths[rle_d_as$values==1][1]))
# [1] 3 4 5 6 7
as <- c(1, 2, 3, 6, 7, 8)
d_as <- diff(as)
rle_d_as <- rle(d_as)
which(d_as==1)[1]+(0:(rle_d_as$lengths[rle_d_as$values==1][1]))
# [1] 1 2 3
A simple way to catch the sequence would be to find the diff of your vector and grab all elements with diff == 1 plus the very next element, i.e.
d1<- which(diff(as) == 1)
as[c(d1, d1[length(d1)]+1)]
NOTE
This will only work If you only have one sequence in your vector. However If we want to make it more general, then I 'd suggest creating a function as so,
get_seq <- function(vec){
d1 <- which(diff(as) == 1)
if(all(diff(d1) == 1)){
return(c(d1, d1[length(d1)]+1))
}else{
d2 <- split(d1, cumsum(c(1, diff(d1) != 1)))[[1]]
return(c(d2, d2[length(d2)]+1))
}
}
#testing it
as <- c(3, 5, 1, 2, 3, 4, 9, 7, 5, 4, 5, 6, 7, 8)
get_seq(as)
#[1] 3 4 5 6
as <- c(8, 9, 10, 11, 1, 2, 3, 4, 7, 8, 9, 10)
get_seq(as)
#[1] 1 2 3 4
as <- c(1, 2, 3, 4, 5, 6, 11)
get_seq(as)
#[1] 1 2 3 4 5 6

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.

Extract several instances of a sequence from vector

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

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