Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

The error is due to the well-known R gotcha that the : (colon operator, which calls seq()) takes higher precedence than arithmetic. Always parenthesize arguments to : if they involve arithmetic or are expressions: a[(i-3):i]

Your code a[i-3:i] doesn't do what you want it to do a[(i-3):i], it does a[i - (3:i)]). So the medians you are printing are for these slices:

4-3:1 # i.e. 1:3
8-3:1 # i.e. 5:7
12-3:1 # i.e. 9:11
16-4:1 # i.e. 13:15

PS some coding-style tips

  • You don't need to iterate over all possible values of i and check them modulo binsize, just do:

    for(i in seq(binsize, length(a), by=binsize)) { ... }

so in your median case:

for(i in seq(binsize, length(a), by=binsize)) {
  print(rep.int(median(a[(i-3):i]),binsize))
}
[1] 36.25 36.25 36.25 36.25
[1] 48.9 48.9 48.9 48.9
[1] 65.25 65.25 65.25 65.25
[1] 79.25 79.25 79.25 79.25
  • But in fact you can replace even that with:

    split(a, ceiling(seq_along(a)/binsize))

as per the "Split a vector into chunks in R""Split a vector into chunks in R"

To make it even clearer, you could define a helper function chunk <- function(x, binsize) { split(x, ceiling(seq_along(x)/binsize)) }

  • Then you can replace the for-loop with sapply:

.

sapply(split(a, ceiling(seq_along(a)/binsize)), mean)
sapply(chunk(a,binsize), mean)
     1      2      3      4 
32.600 49.600 66.175 83.925

sapply(split(a, ceiling(seq_along(a)/binsize)), median)
sapply(chunk(a,binsize), median)
    1     2     3     4 
36.25 48.90 65.25 79.25

Much cleaner, easier to read, and prevents errors, right?

  • The generalization of the colon operator is the seq() function, give it a read, it's pretty useful.

The error is due to the well-known R gotcha that the : (colon operator, which calls seq()) takes higher precedence than arithmetic. Always parenthesize arguments to : if they involve arithmetic or are expressions: a[(i-3):i]

Your code a[i-3:i] doesn't do what you want it to do a[(i-3):i], it does a[i - (3:i)]). So the medians you are printing are for these slices:

4-3:1 # i.e. 1:3
8-3:1 # i.e. 5:7
12-3:1 # i.e. 9:11
16-4:1 # i.e. 13:15

PS some coding-style tips

  • You don't need to iterate over all possible values of i and check them modulo binsize, just do:

    for(i in seq(binsize, length(a), by=binsize)) { ... }

so in your median case:

for(i in seq(binsize, length(a), by=binsize)) {
  print(rep.int(median(a[(i-3):i]),binsize))
}
[1] 36.25 36.25 36.25 36.25
[1] 48.9 48.9 48.9 48.9
[1] 65.25 65.25 65.25 65.25
[1] 79.25 79.25 79.25 79.25
  • But in fact you can replace even that with:

    split(a, ceiling(seq_along(a)/binsize))

as per the "Split a vector into chunks in R"

To make it even clearer, you could define a helper function chunk <- function(x, binsize) { split(x, ceiling(seq_along(x)/binsize)) }

  • Then you can replace the for-loop with sapply:

.

sapply(split(a, ceiling(seq_along(a)/binsize)), mean)
sapply(chunk(a,binsize), mean)
     1      2      3      4 
32.600 49.600 66.175 83.925

sapply(split(a, ceiling(seq_along(a)/binsize)), median)
sapply(chunk(a,binsize), median)
    1     2     3     4 
36.25 48.90 65.25 79.25

Much cleaner, easier to read, and prevents errors, right?

  • The generalization of the colon operator is the seq() function, give it a read, it's pretty useful.

The error is due to the well-known R gotcha that the : (colon operator, which calls seq()) takes higher precedence than arithmetic. Always parenthesize arguments to : if they involve arithmetic or are expressions: a[(i-3):i]

Your code a[i-3:i] doesn't do what you want it to do a[(i-3):i], it does a[i - (3:i)]). So the medians you are printing are for these slices:

4-3:1 # i.e. 1:3
8-3:1 # i.e. 5:7
12-3:1 # i.e. 9:11
16-4:1 # i.e. 13:15

PS some coding-style tips

  • You don't need to iterate over all possible values of i and check them modulo binsize, just do:

    for(i in seq(binsize, length(a), by=binsize)) { ... }

so in your median case:

for(i in seq(binsize, length(a), by=binsize)) {
  print(rep.int(median(a[(i-3):i]),binsize))
}
[1] 36.25 36.25 36.25 36.25
[1] 48.9 48.9 48.9 48.9
[1] 65.25 65.25 65.25 65.25
[1] 79.25 79.25 79.25 79.25
  • But in fact you can replace even that with:

    split(a, ceiling(seq_along(a)/binsize))

as per the "Split a vector into chunks in R"

To make it even clearer, you could define a helper function chunk <- function(x, binsize) { split(x, ceiling(seq_along(x)/binsize)) }

  • Then you can replace the for-loop with sapply:

.

sapply(split(a, ceiling(seq_along(a)/binsize)), mean)
sapply(chunk(a,binsize), mean)
     1      2      3      4 
32.600 49.600 66.175 83.925

sapply(split(a, ceiling(seq_along(a)/binsize)), median)
sapply(chunk(a,binsize), median)
    1     2     3     4 
36.25 48.90 65.25 79.25

Much cleaner, easier to read, and prevents errors, right?

  • The generalization of the colon operator is the seq() function, give it a read, it's pretty useful.
added 302 characters in body
Source Link
smci
  • 331
  • 2
  • 12

The error is due to the well-known R gotcha that the : (colon operator, which calls seq()) takes higher precedence than arithmetic. Always parenthesize arguments to : if they involve arithmetic or are expressions: a[(i-3):i]

Your code a[i-3:i] doesn't do what you want it to do a[(i-3):i], it does a[i - (3:i)]). So the medians you are printing are for these slices:

4-3:1 # i.e. 1:3
8-3:1 # i.e. 5:7
12-3:1 # i.e. 9:11
16-4:1 # i.e. 13:15

PS some coding-style tips

  • You don't need to iterate over all possible values of i and check them modulo binsize, just do:

    for(i in seq(binsize, length(a), by=binsize)) { ... }

Learn all the possible ways to use the seq() function.so in your median case:

for(i in seq(binsize, length(a), by=binsize)) {
  print(rep.int(median(a[(i-3):i]),binsize))
}
[1] 36.25 36.25 36.25 36.25
[1] 48.9 48.9 48.9 48.9
[1] 65.25 65.25 65.25 65.25
[1] 79.25 79.25 79.25 79.25
  • But in fact you can replace even that with:

    split(a, ceiling(seq_along(a)/binsize))

as per the "Split a vector into chunks in R"

To make it even clearer, you could define a helper function chunk <- function(x, binsize) { split(x, ceiling(seq_along(x)/binsize)) }

  • Then you can replace the for-loop with sapply:

.

sapply(split(a, ceiling(seq_along(a)/binsize)), mean)
sapply(chunk(a,binsize), mean)
     1      2      3      4 
32.600 49.600 66.175 83.925

sapply(split(a, ceiling(seq_along(a)/binsize)), median)
sapply(chunk(a,binsize), median)
    1     2     3     4 
36.25 48.90 65.25 79.25

Much cleaner, easier to read, and prevents errors, right?

  • The generalization of the colon operator is the seq() function, give it a read, it's pretty useful.

The error is due to the well-known R gotcha that the : (colon operator, which calls seq()) takes higher precedence than arithmetic. Always parenthesize arguments to : if they involve arithmetic or are expressions: a[(i-3):i]

Your code a[i-3:i] doesn't do what you want it to do a[(i-3):i], it does a[i - (3:i)]). So the medians you are printing are for these slices:

4-3:1 # i.e. 1:3
8-3:1 # i.e. 5:7
12-3:1 # i.e. 9:11
16-4:1 # i.e. 13:15

PS some coding-style tips

  • You don't need to iterate over all possible values of i and check them modulo binsize, just do:

    for(i in seq(binsize, length(a), by=binsize)) { ... }

Learn all the possible ways to use the seq() function.

  • But in fact you can replace even that with:

    split(a, ceiling(seq_along(a)/binsize))

as per the "Split a vector into chunks in R"

To make it even clearer, you could define a helper function chunk <- function(x, binsize) { split(x, ceiling(seq_along(x)/binsize)) }

  • Then you can replace the for-loop with sapply:

.

sapply(split(a, ceiling(seq_along(a)/binsize)), mean)
sapply(chunk(a,binsize), mean)
     1      2      3      4 
32.600 49.600 66.175 83.925

sapply(split(a, ceiling(seq_along(a)/binsize)), median)
sapply(chunk(a,binsize), median)
    1     2     3     4 
36.25 48.90 65.25 79.25

Much cleaner, easier to read, and prevents errors, right?

The error is due to the well-known R gotcha that the : (colon operator, which calls seq()) takes higher precedence than arithmetic. Always parenthesize arguments to : if they involve arithmetic or are expressions: a[(i-3):i]

Your code a[i-3:i] doesn't do what you want it to do a[(i-3):i], it does a[i - (3:i)]). So the medians you are printing are for these slices:

4-3:1 # i.e. 1:3
8-3:1 # i.e. 5:7
12-3:1 # i.e. 9:11
16-4:1 # i.e. 13:15

PS some coding-style tips

  • You don't need to iterate over all possible values of i and check them modulo binsize, just do:

    for(i in seq(binsize, length(a), by=binsize)) { ... }

so in your median case:

for(i in seq(binsize, length(a), by=binsize)) {
  print(rep.int(median(a[(i-3):i]),binsize))
}
[1] 36.25 36.25 36.25 36.25
[1] 48.9 48.9 48.9 48.9
[1] 65.25 65.25 65.25 65.25
[1] 79.25 79.25 79.25 79.25
  • But in fact you can replace even that with:

    split(a, ceiling(seq_along(a)/binsize))

as per the "Split a vector into chunks in R"

To make it even clearer, you could define a helper function chunk <- function(x, binsize) { split(x, ceiling(seq_along(x)/binsize)) }

  • Then you can replace the for-loop with sapply:

.

sapply(split(a, ceiling(seq_along(a)/binsize)), mean)
sapply(chunk(a,binsize), mean)
     1      2      3      4 
32.600 49.600 66.175 83.925

sapply(split(a, ceiling(seq_along(a)/binsize)), median)
sapply(chunk(a,binsize), median)
    1     2     3     4 
36.25 48.90 65.25 79.25

Much cleaner, easier to read, and prevents errors, right?

  • The generalization of the colon operator is the seq() function, give it a read, it's pretty useful.
added 385 characters in body
Source Link
smci
  • 331
  • 2
  • 12

The error is due to the well-known R gotcha that the : (colon operator, which calls seq()) takes lowerhigher precedence than arithmetic. Always parenthesize arguments to : if they involve arithmetic or are expressions: a[(i-3):i]

SoYour code a[i-3:i] doesn't do what you want it to do a[(i-3):i], it does a[i - (3:i)]). So the medians you are printing are offor these slices:

4-3:1 # i.e. 1:3
8-3:1 # i.e. 5:7
12-3:1 # i.e. 9:11
16-4:1 # i.e. 13:15

SOLUTION: always parenthesize argumentsPS some coding-style tips

  • You don't need to iterate over all possible values of i and check them modulo binsize, just do:

    for(i in seq(binsize, length(a), by=binsize)) { ... }

Learn all the possible ways to use the : if they involve arithmetic or are expressions:seq() function.

  • But in fact you can replace even that with:

    split(a, ceiling(seq_along(a)/binsize))

as per the "Split a vector into chunks in R"

To make it even clearer, you could define a helper function a[(ichunk <-3 function(x, binsize):i] { split(x, ceiling(seq_along(x)/binsize)) }

  • Then you can replace the for-loop with sapply:

PS as a coding-style tip, you don't need to iterate over all possible values of i and check them modulo binsize, just do:.

forsapply(isplit(a, inceiling(seq_along(a)/binsize)), seqmean)
sapply(chunk(a,binsize), lengthmean)
     1      2      3      4 
32.600 49.600 66.175 83.925

sapply(split(a, ceiling(seq_along(a)/binsize)), by=binsizemedian)
sapply(chunk(a,binsize), {median)
    1     2     3     4 
36.25 48.90 65.25 }79.25

Learn all the possible waysMuch cleaner, easier to use the seq() function. And you can reaplce the for-loop with sapplyread, and prevents errors, right?

The error is due to the well-known R gotcha that the : (colon operator, which calls seq()) takes lower precedence than arithmetic.

So a[i-3:i] doesn't do what you want it to do a[(i-3):i], it does a[i - (3:i)]). So the medians you are printing are of:

4-3:1 # i.e. 1:3
8-3:1 # i.e. 5:7
12-3:1 # i.e. 9:11
16-4:1 # i.e. 13:15

SOLUTION: always parenthesize arguments to : if they involve arithmetic or are expressions: a[(i-3):i]

PS as a coding-style tip, you don't need to iterate over all possible values of i and check them modulo binsize, just do:

for(i in seq(binsize, length(a), by=binsize)) { ... }

Learn all the possible ways to use the seq() function. And you can reaplce the for-loop with sapply

The error is due to the well-known R gotcha that the : (colon operator, which calls seq()) takes higher precedence than arithmetic. Always parenthesize arguments to : if they involve arithmetic or are expressions: a[(i-3):i]

Your code a[i-3:i] doesn't do what you want it to do a[(i-3):i], it does a[i - (3:i)]). So the medians you are printing are for these slices:

4-3:1 # i.e. 1:3
8-3:1 # i.e. 5:7
12-3:1 # i.e. 9:11
16-4:1 # i.e. 13:15

PS some coding-style tips

  • You don't need to iterate over all possible values of i and check them modulo binsize, just do:

    for(i in seq(binsize, length(a), by=binsize)) { ... }

Learn all the possible ways to use the seq() function.

  • But in fact you can replace even that with:

    split(a, ceiling(seq_along(a)/binsize))

as per the "Split a vector into chunks in R"

To make it even clearer, you could define a helper function chunk <- function(x, binsize) { split(x, ceiling(seq_along(x)/binsize)) }

  • Then you can replace the for-loop with sapply:

.

sapply(split(a, ceiling(seq_along(a)/binsize)), mean)
sapply(chunk(a,binsize), mean)
     1      2      3      4 
32.600 49.600 66.175 83.925

sapply(split(a, ceiling(seq_along(a)/binsize)), median)
sapply(chunk(a,binsize), median)
    1     2     3     4 
36.25 48.90 65.25 79.25

Much cleaner, easier to read, and prevents errors, right?

added 385 characters in body
Source Link
smci
  • 331
  • 2
  • 12
Loading
Source Link
smci
  • 331
  • 2
  • 12
Loading