finding n-1 matrix elements in R - r

probably it's a very simple question with a very simple answer but I just can't figure it out by myself. I have a matrix called 'hz' with 1 column and 115 rows (hz[1:115, 1]) and I'm trying to find the values preceding those that are smaller than 1 and replace them. I did the following:
hz[c(hz < 1)], got 11 values,
then I tried to find the preceding ones: hz[c(hz < 1) - 1], expected 11 values but got 114.
If I try to find specific elements like hz[c(6, 26, 36)], and the preceding ones: hz[c(6, 26, 36) - 1] I got 3 values in both cases as expected. So what's the difference? Is it a problem that I have a condition (<1) in the index?
Thank you for your help!
Viktor

Basically you want hz[which(hz < 1) - 1].
Note, hz < 1 is returning a logical vector, i.e., TRUE / FALSE. If you take subtraction: (hz < 1) - 1, TRUE will be seen as 1 and FALSE will be seen as 0. Applying which to a logical vector gives you positions (as integers) of TRUE, which is what you want.
Consider the following demonstration:
x <- 1:5
x < 3
#[1] TRUE TRUE FALSE FALSE FALSE
(x < 3) - 1
#[1] 0 0 -1 -1 -1
which(x < 3)
#[1] 1 2
which(x < 3) - 1
#[1] 0 1

Related

In R, how does double percent work when used on a matrix?

a <- data.frame(1:50)
a %% 10==0
It returns TRUE for row numbers in increments of 10, starting at 0.
a %% 10==2
This does the same thing, but starting at 2.
How does this syntax work? I know what %% is as an operator but I don't understand what's happening in this scenario.
(Hope I haven't over explained this and interpreted what you're asking correctly:)
As you've said, you know that %% is the modulus operator. So
a <- data.frame(1:50)
a %% 10
returns 'x mod 10' for each item in the frame (or 1,2,3,4,5,6,7,8,9,0 etc etc)
The two examples you've given are like 'shorthand' for if statements. In pseudo code:
for n = 1 to length of a
if a[n] %% 10 equals zero
return true
else
return false
So a %% 10 == 0 is "Does the element mod 10 equal zero" and a %% 10 == 2 is "Does the element mod 10 equal two" applied to each element
And the result is matrix of logicals (booleans).

Fastest way to find switching from positive to negative in a vector in R

I have a vector that contains both positive and negative values. For example something like
x = c(1,2,1,-2,-3,3,-4,5,1,1,-3)
And now I want to flag the indices of the vector where the value changes from positive to negative or negative to positive. So in the example above I would want something a vector of indices that looks something like this
y=c(0,0,0,1,0,1,1,1,0,0,1)
I am doing this in R so if possible I would like to avoid using for-loops.
I think this should work:
+(c(0, diff(sign(x))) != 0)
#[1] 0 0 0 1 0 1 1 1 0 0 1
all.equal(+(c(0, diff(sign(x))) != 0), y)
#[1] TRUE
Here's one way:
yy = rep(0, length(x))
yy[with(rle(sign(x)),{ p = cumsum(c(1,lengths)); p[ -c(1,length(p)) ] })] = 1
all.equal(yy,y) # TRUE
...which turned out more convoluted than I expected at first.

Find the index of negative multiplying of two vectors in R

I have two vectors and I want to find the index of multipling of these two vectors which gets negative and also a[index]is negative and b[index] is positive. How can I find this indexin R?
a = c(1, -1, 2, 3, 4)
b =c(-1, 3, 5, 4, -5)
c = a*b
I have tried this but this is not my desire result:
> which( c <= 0)
[1] 1 2 5
the final result should be index = 1 and 5.
After reading your Question for 10 times, i think your regarded answer is 2 like Simon0101 says.
which( a < 0 & b >= 0 & c < 0)
Please review zour Question or the expected result.

Octave - comparing vectors (element by element)

how do I compare 2 vectors of equal length - I want to get number of elements (which have the same position in both vectors) that differ.
Example:
x=[1 0 0 1 1]
y=[1 0 1 1 0]
result should be 2 since 3rd and 5th element of both vectors differ
One possible solution:
x==y will return a vector of length length(x) (or length(y) since x and y are the same length) with 1 where x(i)==y(i) and 0 where x(i)~=y(i):
>> x==y
ans =
1 1 0 1 0
So all you need to do is sum the elements of x==y and subtract that to length(x)
>> length(x)-sum(x==y)
ans = 2
Arnaud
Compare two matrices (/vectors) with -
z = eq(x, y) % returns 1 for match and 0 for mismatch
which returns a matrix z of 0s and 1s. Finally count the number of zeros in it:
sum(z == 0); % find total non matching elements
sum(ne(x, y)) % find all elements that are different
gives 2

Creation of a specific vector without loop or recursion in R

I've got a first vector, let's say x that consists only of 1's and -1's. Then, I have a second vector y that consists of 1's, -1's, and zeros. Now, I'd like to create a vector z that contains in index i a 1 if x[i] equals 1 and a 1 exists within the vector y between the n precedent elements (y[(i-n):i])...
more formally: z <- ifelse(x == 1 && 1 %in% y[(index(y)-n):index(y)],1,0)
I'm looking to create such a vector in R without looping or recursion. The proposition above does not work since it does not recognize to take the expression y[(index(y)-n):index(y)] element by element.
Thanks a lot for your support
Here's an approach that uses the cumsum function to test for the number of ones that have been seen so far. If the number of ones at position i is larger than the number of ones at position i-n, then the condition on the right will be satisfied.
## Generate some random y's.
> y <- sample(-1:1, 25, replace=T)
> y
[1] 0 1 -1 -1 -1 -1 -1 1 -1 -1 -1 -1 0 0 -1 -1 -1 1 -1 1 1 0 0 0 1
> n <- 3
## Compute number of ones seen at each position.
> cs <- cumsum(ifelse(y == 1, 1, 0))
> lagged.cs <- c(rep(0, n), cs[1:(length(cs)-n)])
> (cs - lagged.cs) > 0
[1] FALSE TRUE TRUE TRUE FALSE FALSE FALSE TRUE TRUE TRUE FALSE FALSE
[13] FALSE FALSE FALSE FALSE FALSE TRUE TRUE TRUE TRUE TRUE TRUE FALSE
[25] TRUE
You could use apply like this, although it is essentially a pretty way to do a loop, I'm not sure if it will be faster (it may or may not).
y1 <- unlist(lapply(1:length(x), function(i){1 %in% y[max(0, (i-n)):i]}))
z <- as.numeric(x==1) * as.numeric(y1)

Resources