I try to give value for a 2 dimensional matrix using for loop in R, however it gives me unexpected NA values

daluobo

I want to calculate the moving sum with varying window sizes of 1:15.

a <- matrix(0,257,15)
b <- c(1:257)
for(j in 1:15) {
  for(i in j:257) {
    a[i,j] <- sum(b[i-j+1:i])
  }
}

However, the above code gives cases me confusion, as it yields NA after the 129th row in every column. What could be reason for such behaviour?

Heikki

Add parentheses (...):i into indexing of b[(i-j+1):i] in order to properly have a range between i-j+1 and i. The full code then reads as

a <- matrix(0,257,15)
b <- c(1:257)
for (j in 1:15) {
  for (i in j:257) {
    a[i,j] <- sum(b[(i-j+1):i])
  }
}

As an example on the importance of the parentheses, you may compare the calculating order of the following three cases:

> (1+1):2
[1] 2
> 1+1:2
[1] 2 3
> 1+(1:2)
[1] 2 3

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Using random matrix in the loop (R)

From Dev

R loop replace multiple values in matrix

From Dev

Convert 2 columns dataframe to a 2 dimensional matrix in R

From Dev

R - Data frame to matrix for 2-dimensional values?

From Dev

Using factor with levels gives me NA

From Dev

getStringArrayListExtra give me a null value but I have values on it

From Dev

How can i get the n-2-dimensional submatrix from a n-dimensional matrix?

From Dev

how to change values with rank value (ordered value) in R? and I want to give same numbers when two values tie

From Dev

Whenever i try moving button it gives me XML error at runtime

From Dev

Sequentially amending row values in a matrix using loop in R

From Dev

SVD in a term document matrix do not give me values I want

From Dev

R - using apply on numeric matrix with shapiro.test() gives error: all 'x' values are identical

From Dev

How do I create an empty matrix in R and update it using a for loop?

From Dev

How do I loop across a correlation matrix to only give me pairs of correlations above a certain threshold? And/or make it more efficient

From Dev

ggplot2 gives me the blues in R in scatter plots, how do I do a spectral color set?

From Dev

Replicating 2 dimensional matrix to create a 3 dimensional array (in R)

From Dev

How can I print a matrix in R with NA values hidden?

From Dev

Java 2 dimensional array value and for loop problems

From Dev

Conditionally Fill Matrix based on values of another Matrix using For loop

From Dev

R loop replace multiple values in matrix

From Dev

Convert 2 columns dataframe to a 2 dimensional matrix in R

From Dev

I try to ge the sum of 2 values using + but It does not work

From Dev

how to change value of matrix with for loop using another matrix in r?

From Dev

R - How to normalize the values in matrix using R

From Dev

Why does xrandr give me errors if I try and use commands on my computer, but not if I ssh into it?

From Dev

How can I get a max value of each column in matrix using For Loop in R

From Dev

Making an array of ints from a String using charAt(i) gives me 2 digit numbers in each index?

From Dev

When I try to save to ui colors with userDefaults, Xcode gives me the error : Fatal error: Unexpectedly found nil while unwrapping an Optional value

From Dev

SQL or mariadb gives me this error when I try to create a table

Related Related

  1. 1

    Using random matrix in the loop (R)

  2. 2

    R loop replace multiple values in matrix

  3. 3

    Convert 2 columns dataframe to a 2 dimensional matrix in R

  4. 4

    R - Data frame to matrix for 2-dimensional values?

  5. 5

    Using factor with levels gives me NA

  6. 6

    getStringArrayListExtra give me a null value but I have values on it

  7. 7

    How can i get the n-2-dimensional submatrix from a n-dimensional matrix?

  8. 8

    how to change values with rank value (ordered value) in R? and I want to give same numbers when two values tie

  9. 9

    Whenever i try moving button it gives me XML error at runtime

  10. 10

    Sequentially amending row values in a matrix using loop in R

  11. 11

    SVD in a term document matrix do not give me values I want

  12. 12

    R - using apply on numeric matrix with shapiro.test() gives error: all 'x' values are identical

  13. 13

    How do I create an empty matrix in R and update it using a for loop?

  14. 14

    How do I loop across a correlation matrix to only give me pairs of correlations above a certain threshold? And/or make it more efficient

  15. 15

    ggplot2 gives me the blues in R in scatter plots, how do I do a spectral color set?

  16. 16

    Replicating 2 dimensional matrix to create a 3 dimensional array (in R)

  17. 17

    How can I print a matrix in R with NA values hidden?

  18. 18

    Java 2 dimensional array value and for loop problems

  19. 19

    Conditionally Fill Matrix based on values of another Matrix using For loop

  20. 20

    R loop replace multiple values in matrix

  21. 21

    Convert 2 columns dataframe to a 2 dimensional matrix in R

  22. 22

    I try to ge the sum of 2 values using + but It does not work

  23. 23

    how to change value of matrix with for loop using another matrix in r?

  24. 24

    R - How to normalize the values in matrix using R

  25. 25

    Why does xrandr give me errors if I try and use commands on my computer, but not if I ssh into it?

  26. 26

    How can I get a max value of each column in matrix using For Loop in R

  27. 27

    Making an array of ints from a String using charAt(i) gives me 2 digit numbers in each index?

  28. 28

    When I try to save to ui colors with userDefaults, Xcode gives me the error : Fatal error: Unexpectedly found nil while unwrapping an Optional value

  29. 29

    SQL or mariadb gives me this error when I try to create a table

HotTag

Archive