I'm trying to understand how the %% operator works in R:
10 %% 10 # 0
20 %% 10 # 0
I'm not sure about these two results:
10 %% 20 # 10
2 %% 8 # 2
Can you help me understand the last two results? I'm a little confused.
Nothing wrong:
10 = 1 * 10 + 0
20 = 2 * 10 + 0
10 = 0 * 20 + 10
2 = 0 * 8 + 2
The modulo is the number after +.
In general, for two numbers a and b, there is
a = floor(a / b) * b + (a %% b)
Let's write a toy function:
foo <- function(a,b) c(quotient = floor(a / b), modulo = a %% b)
foo(10, 10)
#quotient modulo
# 1 0
foo(20, 10)
#quotient modulo
# 2 0
foo(10, 20)
#quotient modulo
# 0 10
foo(2, 8)
#quotient modulo
# 0 2
Update: Instead of using floor(a / b) to get quotient, we can also use a %/% b.
I'll offer another explanation. Take this problem:
20 %% 10 = 0
Instead of evaluating the modulo, start with simple divison:
20 / 10 = 2
As you know, the answer "2" means that it takes two sets of 10 to get 20. Note that we can also write the answer this way with the decimal, 2.0.
The decimal is important. When the decimal is .0, we have no remainder. We have complete sets. If division yields a 0 decimal, then the modulo evaluates to zero.
Now consider this:
11/3 = 3.667
That tail part, the 0.667, is the portion of a set of 3 that remains after we form all full sets of 3 that we can. On the left side of the decimal, we show:
#Splitting the answer into its components - 3 full sets, 0.667 partial sets
3.0 + 0.667 = 3.667
So if we want to know the actual remaining quantity, we can multiply 0.667 by the divisor, 3:
0.667 * 3 = 2
This is the remainder. It is the quantity that remains after all full sets of 3 are formed. It's the same result we get using modulo:
11 %% 3 = 2
The same applies here. Given this problem,
10 %% 20 = 10
we can divide normally and get:
10 / 20 = 0.5
Reading this out, we have 0 full groups of 20 (left side); we only have half a set, 0.5, of 20.
0.5 * 20 = 10
This is equivalent to:
10 %% 20 = 10
10 is thus the remainder. It's the gap between the 10 we have and the 10 we need to get to 20.
I was also very confused, but if you understand that the result of the %% operator is the REMAINDER of a division, it is very easy.
Eg. 75%%4 = 3
and I noticed if the dividend is lower than the divisor, then R returns the same dividend value.
Eg.
4%%75 = 4
10 %% 20 = 10
2 %% 8 = 2
Syntax
remainder <- dividend %% divisor
Details
The only thing that were missing from the documentations were the details on which side is the dividend and which side is the divisor. Wikipedia describes the two terms as:
What is being divided is called the dividend, which is divided by the divisor, and the result is called the quotient. In the example, 20 is the dividend, 5 is the divisor, and 4 is the quotient.
However, in comparison with the division operation, the modulo operation is not returning the quotient. Instead, it is returning the remainder.
Examples
To easily understand the modulo operation, ideally the dividend > divisor.
12 %% 11
# quotient is 1.090909
# remainder is 1
12 %% 10
# quotient is 1.2
# remainder is 2
12 %% 9
# quotient is 1.333333
# remainder is 3
12 %% 8
# quotient is 1.5
# remainder is 4
12 %% 7
# quotient is 1.714286
# remainder is 5
12 %% 6
# quotient is 2
# remainder is 0
# 12 is divisible by 6
12 %% 5
# quotient is 2.4
# remainder is 2
12 %% 4
# quotient is 3
# remainder is 0
# 12 is divisible by 4
12 %% 3
# quotient is 4
# remainder is 0
# 12 is divisible by 3
12 %% 2
# quotient is 6
# remainder is 0
# 12 is divisible by 2
12 %% 1
# quotient is 12
# remainder is 0
# any whole number is divisible by 1
Trying to understand some results in R with x modulo y I found this page. Then trying to explain to myself some "quirky" results I wrote this R script below. I had read that the remainder or result of modulo operator is supposed to be always positive, but this is not the case in R, and the definition and example provide here explain the logic that seems to be used. Definition x mod y = x - ( |_x/y_| * y) where |_x/y_| = floor(x/y) seems to always be true in R, or in a more standard way, the definition of the remainder r of the operation q = x / y is x = k*q + r, where k and r both are integers.
Basically in R with x = 2 and y = - 5, x mod y = -3; or using definition x = k*q + r we have r = x - k*q = -3.
Still, this is kind of quirky in a mathematical sense because "integer part product" (k*q) actually exceeds the dividend (x), thus defining the remainder (r) as a negative integer...
x <- 2
y <- -5
q <- x/y
k <- floor(2/-5)
kq <- floor(2/-5) * -5
r <- 2 - (floor(2/-5) * -5)
x %% y
Related
This
n = length(s)
# n = 25920169
nfft = 8192
noverlap = Int64(floor(nfft/2))
window = hanning(nfft)
#sp = spectrogram(s, n, noverlap; nfft=nfft, fs=1, window=window)
sp = periodogram(s; nfft=nfft, fs=1, window=window)
throws the error
nfft must be >= n
But the documentation says:
If length(s) < nfft, then the input is padded with zeros.
Doesn't it mean that nfft < n should be correct?
I think that the FFT length nfft should be greater than the signal length n to prevent aliasing.
The periodogram function uses FFT internally, where the length is denoted as nfft. In theory, when using FFT, the signal in both time domain and frequency domain are discrete and periodic, where the period is given by nfft. So, if you specify an nfft that is less than the signal length n, this actually introduces aliasing in the time domain to make the signal periodic with nfft.
For example, if you have a sequence 1 2 3 4 5, assuming that your period is also 5, you have
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
--------------------------------
... 1 2 3 4 5 ...
i.e., the original sequence. Now assume you have a period of 3, then it looks like
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
------------------------
... 5 7 3 ...
When you take FFT of this sequence with n > nfft, you are working with this aliased sequence.
You can manually allow for n > nfft by applying the wrap(x,nfft) as bellow and feeding its output to periodogram, MATLAB does exactly that.
function wrap(x,nfft)
y = zeros(eltype(x),nfft)
for (i,xi) in enumerate(x)
y[mod1(i,nfft)] += xi
end
y
end
For example:
wrap(1:5,3)
3-element Vector{Int64}:
5
7
3
This question already has an answer here:
subtract every value in a vector from every other value in the vector R
(1 answer)
Closed 2 years ago.
I have the following vector in R
x<-c(5,7,8,20,11,30)
I want to get all the possible subtractions xi-xj, 1<=i<j<=6. I used outer function but I got a matrix with zero diagonal. I do not want to have xi-xj with i=j. Additionally, I want to have a vector, not a matrix.
You can subset the output of outer using lower.tri or upper.tri.
y <- outer(x, x, '-')
y[lower.tri(y)]
# [1] 2 3 15 6 25 1 13 4 23 12 3 22 -9 10 19
Alternatively you can generate the indices using seq and rep.
k <- seq(length(x) - 1, 1)
i <- rep(seq_along(k), k)
j <- sequence(k) + i
x[j] - x[i]
# [1] 2 3 15 6 25 1 13 4 23 12 3 22 -9 10 19
Yes, outer generates a matrix with all possible combinations in both the directions (a - b and b - a) which is unnecessary in this case.
Perhaps, you can use combn :
combn(x, 2, diff)
#[1] 2 3 15 6 25 1 13 4 23 12 3 22 -9 10 19
Also as #Rui Barradas points out that outer might not be the right solution since it fails for the condition 1<=i<j<=6.
Higher order functions; less succinct/readable than above but quicker for some reason.
Filter(function(y) {y != 0},
do.call("c", (Map(function(i){y <- x - x[i]; y[i:length(x)]}, seq_along(x)))))
I would like to know if there is an efficient way to know if which values of my column in my dataframe are power of two.
My data is a dataframe with 6 columns, one of the columns has the values that I want to check if the numbers are power of 2.
class(df$doubling_times) > numeric
log2(x) %% 1 == 0 - checks if the log base 2 of the number is an integer (when divided by 1, is the remainder 0?)
> x = 1:10
> data.frame(x, power2 = log2(x) %% 1 == 0)
x power2
1 1 TRUE
2 2 TRUE
3 3 FALSE
4 4 TRUE
5 5 FALSE
6 6 FALSE
7 7 FALSE
8 8 TRUE
9 9 FALSE
10 10 FALSE
The above should work, but a safer approach would allow for floating point accuracy issues, and might be something like this:
remainder = log2(x) %% 1
tol = 1e-12 # tolerance
power2 = abs(remainder - round(remainder)) < tol
I've read most of the similar questions here, but I'm still having a hard time understanding how passing arguments in the order function break ties.
The example introduced in the R documentation shows that :
order(x <- c(1,1,3:1,1:4,3), y <- c(9,9:1), z <- c(2,1:9))
returns
[1] 6 5 2 1 7 4 10 8 3 9
However, what does it mean when y is 'breaking ties' of x, and z 'breaking ties' of y? the x vector is:
[1] 1 1 3 2 1 1 2 3 4 3
and the y vector is:
[1] 9 9 8 7 6 5 4 3 2 1
Also, if I eliminate z from the first function,
order(x <- c(1,1,3:1,1:4,3), y <- c(9,9:1))
it returns :
[1] 6 5 1 2 7 4 10 8 3 9
so I'm unclear how the numbers in the y vector are relevant with ordering the four 1s, the two 2s, and the three 3s in x. I would very much appreciate the help. Thanks!
Let's take a look at
idx <- order(x <- c(1,1,3:1,1:4,3), y <- c(9,9:1), z <- c(2,1:9))
idx;
#[1] 6 5 2 1 7 4 10 8 3 9
First thing to note is that
x[idx]
# [1] 1 1 1 1 2 2 3 3 3 4
So idx orders entries in x from smallest to largest values.
Values in y and z affect how order treats ties in x.
Take entries x[5] = 1 and x[6] = 1. Since there is a tie here, order looks up entries at the corresponding positions in y, i.e. y[5] = 6 and y[6] = 5. Since y[6] < y[5], the entries in x are sorted x[6] < x[5].
If there is a tie in y as well, order will look up entries in the next vector z. This happens for x[1] = 1 and x[2] = 2, where both y[1] = 9 and y[2] = 9. Here z breaks the tie because z[2] = 1 < z[1] = 2 and therefore x[2] < x[1].
I'm trying to understand how the %% operator works in R:
10 %% 10 # 0
20 %% 10 # 0
I'm not sure about these two results:
10 %% 20 # 10
2 %% 8 # 2
Can you help me understand the last two results? I'm a little confused.
Nothing wrong:
10 = 1 * 10 + 0
20 = 2 * 10 + 0
10 = 0 * 20 + 10
2 = 0 * 8 + 2
The modulo is the number after +.
In general, for two numbers a and b, there is
a = floor(a / b) * b + (a %% b)
Let's write a toy function:
foo <- function(a,b) c(quotient = floor(a / b), modulo = a %% b)
foo(10, 10)
#quotient modulo
# 1 0
foo(20, 10)
#quotient modulo
# 2 0
foo(10, 20)
#quotient modulo
# 0 10
foo(2, 8)
#quotient modulo
# 0 2
Update: Instead of using floor(a / b) to get quotient, we can also use a %/% b.
I'll offer another explanation. Take this problem:
20 %% 10 = 0
Instead of evaluating the modulo, start with simple divison:
20 / 10 = 2
As you know, the answer "2" means that it takes two sets of 10 to get 20. Note that we can also write the answer this way with the decimal, 2.0.
The decimal is important. When the decimal is .0, we have no remainder. We have complete sets. If division yields a 0 decimal, then the modulo evaluates to zero.
Now consider this:
11/3 = 3.667
That tail part, the 0.667, is the portion of a set of 3 that remains after we form all full sets of 3 that we can. On the left side of the decimal, we show:
#Splitting the answer into its components - 3 full sets, 0.667 partial sets
3.0 + 0.667 = 3.667
So if we want to know the actual remaining quantity, we can multiply 0.667 by the divisor, 3:
0.667 * 3 = 2
This is the remainder. It is the quantity that remains after all full sets of 3 are formed. It's the same result we get using modulo:
11 %% 3 = 2
The same applies here. Given this problem,
10 %% 20 = 10
we can divide normally and get:
10 / 20 = 0.5
Reading this out, we have 0 full groups of 20 (left side); we only have half a set, 0.5, of 20.
0.5 * 20 = 10
This is equivalent to:
10 %% 20 = 10
10 is thus the remainder. It's the gap between the 10 we have and the 10 we need to get to 20.
I was also very confused, but if you understand that the result of the %% operator is the REMAINDER of a division, it is very easy.
Eg. 75%%4 = 3
and I noticed if the dividend is lower than the divisor, then R returns the same dividend value.
Eg.
4%%75 = 4
10 %% 20 = 10
2 %% 8 = 2
Syntax
remainder <- dividend %% divisor
Details
The only thing that were missing from the documentations were the details on which side is the dividend and which side is the divisor. Wikipedia describes the two terms as:
What is being divided is called the dividend, which is divided by the divisor, and the result is called the quotient. In the example, 20 is the dividend, 5 is the divisor, and 4 is the quotient.
However, in comparison with the division operation, the modulo operation is not returning the quotient. Instead, it is returning the remainder.
Examples
To easily understand the modulo operation, ideally the dividend > divisor.
12 %% 11
# quotient is 1.090909
# remainder is 1
12 %% 10
# quotient is 1.2
# remainder is 2
12 %% 9
# quotient is 1.333333
# remainder is 3
12 %% 8
# quotient is 1.5
# remainder is 4
12 %% 7
# quotient is 1.714286
# remainder is 5
12 %% 6
# quotient is 2
# remainder is 0
# 12 is divisible by 6
12 %% 5
# quotient is 2.4
# remainder is 2
12 %% 4
# quotient is 3
# remainder is 0
# 12 is divisible by 4
12 %% 3
# quotient is 4
# remainder is 0
# 12 is divisible by 3
12 %% 2
# quotient is 6
# remainder is 0
# 12 is divisible by 2
12 %% 1
# quotient is 12
# remainder is 0
# any whole number is divisible by 1
Trying to understand some results in R with x modulo y I found this page. Then trying to explain to myself some "quirky" results I wrote this R script below. I had read that the remainder or result of modulo operator is supposed to be always positive, but this is not the case in R, and the definition and example provide here explain the logic that seems to be used. Definition x mod y = x - ( |_x/y_| * y) where |_x/y_| = floor(x/y) seems to always be true in R, or in a more standard way, the definition of the remainder r of the operation q = x / y is x = k*q + r, where k and r both are integers.
Basically in R with x = 2 and y = - 5, x mod y = -3; or using definition x = k*q + r we have r = x - k*q = -3.
Still, this is kind of quirky in a mathematical sense because "integer part product" (k*q) actually exceeds the dividend (x), thus defining the remainder (r) as a negative integer...
x <- 2
y <- -5
q <- x/y
k <- floor(2/-5)
kq <- floor(2/-5) * -5
r <- 2 - (floor(2/-5) * -5)
x %% y