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

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)

Related

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

Arithmetic operation (Difference) between a vector and a Matrix in R

I am facing this issue when I am trying to calculate the difference between list and matrix in R, it is giving me weird results.
matrix <- matrix(1:10,1:10,nrow = 10,ncol=10)
list1 <- seq(1:10)
diff <- list1-matrix
Below is the output that I am trying to achieve but it does not work. Please let me know what I am doing wrong.
We can do this with multiple ways
1) Transpose the matrix, subtract from the vector and then do the transpose
t(t(matrix)- list1)
2) replicate the vector to make the lengths same and then do the difference
matrix - list1[col(matrix)]
3) Using sweep
sweep(matrix, 2, list1, `-`)
What you need to understand is that in R matrices are stored in column-major order. This means that a matrix is a sequence of numbers order by columns from top to down.
So when you subtract a vector from a matrix you can imagine it as operation with 4 steps:
matrix is turned into a sequence of numbers (a vector)
your "list1" is expanded (repeated) so that it's length matches the length of the vector constructed from the matrix.
the elements in each are subtracted from each other
the numbers are assembled into the matrix form again.
In your case:
1) matrix is turned into a sequence of numbers (by columns) giving you:
1, 1, 1 ,1 ... 1, 2, 2, 2, 2, ... 2, ... ... 10, 10, 10, 10, ... 10
2) Your second vector is repeated until it matches the first one:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1 ,2 ,3 ,4 ,5 ,6 ,7 ,8, 9, 10, ... 1, 2, 3, ... 10
3) Now both of them have the same length so they are subtracted from each other:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, ..., -9, -8, -7, ... 0
4) The result is transformed into a matrix again giving you the matrix that you see.
In order to obtain your desired result you have to take this into account. One way is to transpose your matrix, do the subtraction, and transpose it back again:
t(t(matrix) - list1)
This works because after the transpose t the rows are turned into columns.
Another approach is to construct a matrix out of your list1 vector:
matrix2 <- matrix(list1, nrow=10, ncol=10, byrow=TRUE)
And then do the subtraction:
diff <- matrix - matrix2

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.

ifelse statement in R

I'm looking at a gene in 10 people. And this gene has two alleles, say a and b. And each allele has 3 forms: type 2, 3 or 4.
a <- c(2, 2, 2, 2, 3, 3, 3, 2, 4, 3)
b <- c(4, 2, 3, 2, 4, 2, 3, 4, 4, 4)
I wish to code a variable that tells me how many type 4 alleles the person has: 0, 1, or 2.
var <- ifelse(a==4 & b==4, 2, 0)
The code above doesn't work since I didn't account for the individuals who have just one copy of the type 4 allele. I feel like I might need 2 ifelse statements that work simultaneously?
EDIT: You don't actually need ifelse or any fancy operations other than plus and equal to.
var <- (a == 4) + (b == 4)
If you're set on ifelse, this can be done with
var <- ifelse(a == 4, 1, 0) + ifelse(b == 4, 1, 0)
However, I prefer the following solution using apply. The following will give you three cases, the result being the number of 4's the person has (assuming each row is a person).
a = c(2, 2, 2, 2, 3, 3, 3, 2, 4, 3)
b = c(4, 2, 3, 2, 4, 2, 3, 4, 4, 4)
d <- cbind(a,b)
apply(d, 1, function(x) {sum(x == 4)})
For this operation, I first combined the two vectors into a matrix since it makes applying the function easier. In R, generally if data are the same type it is easier (and faster for the computer) to combine the data into a matrix/data frame/etc., then create a function to be performed on each row/column/etc.
To understand the output, consider what happens to the first row of d.
> d[1, ]
a b
2 4
> d[1, ] == 4
a b
FALSE TRUE
Booleans are interpreted as integers under addition, so
> FALSE + TRUE
[1] 1
It doesn't seem to matter whether the 4 came from a or b, so we end up with three cases: 0, 1, and 2, depending on the number of 4's.

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