Unable to store data in a matrix












1















I am using the following code to check P-values of a linear trend, but it seems that the loop is not working properly as I cannot see a 2-D map of P-value but only a row



library(chron)
library(RColorBrewer)
library(lattice)
library(ncdf4)
#-------------------------------------------------------------------------------------------
options(warn=-1)

ncin <- nc_open("MOD04_10K_Winter.nc", readunlim=FALSE)
#print(ncin)
lon <- ncvar_get(ncin, varid="Longitude", start=NA, count=NA, verbose=FALSE,
signedbyte=TRUE, collapse_degen=TRUE, raw_datavals=FALSE )
lat <- ncvar_get(ncin, varid="Latitude", start=NA, count=NA, verbose=FALSE,
signedbyte=TRUE, collapse_degen=TRUE, raw_datavals=FALSE )
aod <- ncvar_get(ncin, varid="AOD", start=NA, count=NA, verbose=FALSE,
signedbyte=TRUE, collapse_degen=TRUE, raw_datavals=FALSE )

px <- matrix(nrow = 1:length(lon), ncol = 1:length(lat))
is.matrix(px)

for (lo in 1:length(lon)) {
for (la in 1:length(lat)) {
int1a = aod[lo, la,]

# if mean of int is finite then proceed else fill NA to all arrays
mn = mean(int1a, trim = 0, na.rm = FALSE)
if (is.finite(mn))
{
print("---------------- Reading Finite data -------------")
xs = 1:30
fn1a = lm(int1a~xs) # Function_NCP
p_val = summary(fn1a)$coefficients[2, 4] # Saving p-value
if (p_val < 0.05) {print("statisticlly significant")} else {print("statisticlly in-significant")}
print(p_val)
print(lo)
print(la)
px[lo][la] = p_val # variables in only (?)
}

} # latitude dimension
}


If I am using [lo, la] instead of [lo][la] I am having the following error




Error in [<-(*tmp*, lo, la, value = 0.0543481042240582) :

subscript out of bounds




Sorry if the solution is very trivial, I have just started working in R.










share|improve this question

























  • This just means that somewhere, in one of the px a number is going in that doesn't correspond to anything in px. Try checking the extreme values on the for loop (ie the first number that gets run through and the last) as they are usually the cause of problems like this. Also if you want more detailed help make your answer reproducible

    – RAB
    Nov 22 '18 at 7:18


















1















I am using the following code to check P-values of a linear trend, but it seems that the loop is not working properly as I cannot see a 2-D map of P-value but only a row



library(chron)
library(RColorBrewer)
library(lattice)
library(ncdf4)
#-------------------------------------------------------------------------------------------
options(warn=-1)

ncin <- nc_open("MOD04_10K_Winter.nc", readunlim=FALSE)
#print(ncin)
lon <- ncvar_get(ncin, varid="Longitude", start=NA, count=NA, verbose=FALSE,
signedbyte=TRUE, collapse_degen=TRUE, raw_datavals=FALSE )
lat <- ncvar_get(ncin, varid="Latitude", start=NA, count=NA, verbose=FALSE,
signedbyte=TRUE, collapse_degen=TRUE, raw_datavals=FALSE )
aod <- ncvar_get(ncin, varid="AOD", start=NA, count=NA, verbose=FALSE,
signedbyte=TRUE, collapse_degen=TRUE, raw_datavals=FALSE )

px <- matrix(nrow = 1:length(lon), ncol = 1:length(lat))
is.matrix(px)

for (lo in 1:length(lon)) {
for (la in 1:length(lat)) {
int1a = aod[lo, la,]

# if mean of int is finite then proceed else fill NA to all arrays
mn = mean(int1a, trim = 0, na.rm = FALSE)
if (is.finite(mn))
{
print("---------------- Reading Finite data -------------")
xs = 1:30
fn1a = lm(int1a~xs) # Function_NCP
p_val = summary(fn1a)$coefficients[2, 4] # Saving p-value
if (p_val < 0.05) {print("statisticlly significant")} else {print("statisticlly in-significant")}
print(p_val)
print(lo)
print(la)
px[lo][la] = p_val # variables in only (?)
}

} # latitude dimension
}


If I am using [lo, la] instead of [lo][la] I am having the following error




Error in [<-(*tmp*, lo, la, value = 0.0543481042240582) :

subscript out of bounds




Sorry if the solution is very trivial, I have just started working in R.










share|improve this question

























  • This just means that somewhere, in one of the px a number is going in that doesn't correspond to anything in px. Try checking the extreme values on the for loop (ie the first number that gets run through and the last) as they are usually the cause of problems like this. Also if you want more detailed help make your answer reproducible

    – RAB
    Nov 22 '18 at 7:18
















1












1








1








I am using the following code to check P-values of a linear trend, but it seems that the loop is not working properly as I cannot see a 2-D map of P-value but only a row



library(chron)
library(RColorBrewer)
library(lattice)
library(ncdf4)
#-------------------------------------------------------------------------------------------
options(warn=-1)

ncin <- nc_open("MOD04_10K_Winter.nc", readunlim=FALSE)
#print(ncin)
lon <- ncvar_get(ncin, varid="Longitude", start=NA, count=NA, verbose=FALSE,
signedbyte=TRUE, collapse_degen=TRUE, raw_datavals=FALSE )
lat <- ncvar_get(ncin, varid="Latitude", start=NA, count=NA, verbose=FALSE,
signedbyte=TRUE, collapse_degen=TRUE, raw_datavals=FALSE )
aod <- ncvar_get(ncin, varid="AOD", start=NA, count=NA, verbose=FALSE,
signedbyte=TRUE, collapse_degen=TRUE, raw_datavals=FALSE )

px <- matrix(nrow = 1:length(lon), ncol = 1:length(lat))
is.matrix(px)

for (lo in 1:length(lon)) {
for (la in 1:length(lat)) {
int1a = aod[lo, la,]

# if mean of int is finite then proceed else fill NA to all arrays
mn = mean(int1a, trim = 0, na.rm = FALSE)
if (is.finite(mn))
{
print("---------------- Reading Finite data -------------")
xs = 1:30
fn1a = lm(int1a~xs) # Function_NCP
p_val = summary(fn1a)$coefficients[2, 4] # Saving p-value
if (p_val < 0.05) {print("statisticlly significant")} else {print("statisticlly in-significant")}
print(p_val)
print(lo)
print(la)
px[lo][la] = p_val # variables in only (?)
}

} # latitude dimension
}


If I am using [lo, la] instead of [lo][la] I am having the following error




Error in [<-(*tmp*, lo, la, value = 0.0543481042240582) :

subscript out of bounds




Sorry if the solution is very trivial, I have just started working in R.










share|improve this question
















I am using the following code to check P-values of a linear trend, but it seems that the loop is not working properly as I cannot see a 2-D map of P-value but only a row



library(chron)
library(RColorBrewer)
library(lattice)
library(ncdf4)
#-------------------------------------------------------------------------------------------
options(warn=-1)

ncin <- nc_open("MOD04_10K_Winter.nc", readunlim=FALSE)
#print(ncin)
lon <- ncvar_get(ncin, varid="Longitude", start=NA, count=NA, verbose=FALSE,
signedbyte=TRUE, collapse_degen=TRUE, raw_datavals=FALSE )
lat <- ncvar_get(ncin, varid="Latitude", start=NA, count=NA, verbose=FALSE,
signedbyte=TRUE, collapse_degen=TRUE, raw_datavals=FALSE )
aod <- ncvar_get(ncin, varid="AOD", start=NA, count=NA, verbose=FALSE,
signedbyte=TRUE, collapse_degen=TRUE, raw_datavals=FALSE )

px <- matrix(nrow = 1:length(lon), ncol = 1:length(lat))
is.matrix(px)

for (lo in 1:length(lon)) {
for (la in 1:length(lat)) {
int1a = aod[lo, la,]

# if mean of int is finite then proceed else fill NA to all arrays
mn = mean(int1a, trim = 0, na.rm = FALSE)
if (is.finite(mn))
{
print("---------------- Reading Finite data -------------")
xs = 1:30
fn1a = lm(int1a~xs) # Function_NCP
p_val = summary(fn1a)$coefficients[2, 4] # Saving p-value
if (p_val < 0.05) {print("statisticlly significant")} else {print("statisticlly in-significant")}
print(p_val)
print(lo)
print(la)
px[lo][la] = p_val # variables in only (?)
}

} # latitude dimension
}


If I am using [lo, la] instead of [lo][la] I am having the following error




Error in [<-(*tmp*, lo, la, value = 0.0543481042240582) :

subscript out of bounds




Sorry if the solution is very trivial, I have just started working in R.







r loops






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 16:29









Ekatef

72149




72149










asked Nov 22 '18 at 5:44









piyush bhardwajpiyush bhardwaj

194




194













  • This just means that somewhere, in one of the px a number is going in that doesn't correspond to anything in px. Try checking the extreme values on the for loop (ie the first number that gets run through and the last) as they are usually the cause of problems like this. Also if you want more detailed help make your answer reproducible

    – RAB
    Nov 22 '18 at 7:18





















  • This just means that somewhere, in one of the px a number is going in that doesn't correspond to anything in px. Try checking the extreme values on the for loop (ie the first number that gets run through and the last) as they are usually the cause of problems like this. Also if you want more detailed help make your answer reproducible

    – RAB
    Nov 22 '18 at 7:18



















This just means that somewhere, in one of the px a number is going in that doesn't correspond to anything in px. Try checking the extreme values on the for loop (ie the first number that gets run through and the last) as they are usually the cause of problems like this. Also if you want more detailed help make your answer reproducible

– RAB
Nov 22 '18 at 7:18







This just means that somewhere, in one of the px a number is going in that doesn't correspond to anything in px. Try checking the extreme values on the for loop (ie the first number that gets run through and the last) as they are usually the cause of problems like this. Also if you want more detailed help make your answer reproducible

– RAB
Nov 22 '18 at 7:18














1 Answer
1






active

oldest

votes


















0














You have just to make a small fix on the matrix px declaration. Now you set the number of rows and columns as vectors: nrow = 1:length(lon) and nrow = 1:length(lon). R silently takes only the first elements of these vectors and generates a 1 to 1 matrix. (Actually, it would generate a warning, by the warnings are supressed!)



So, the solution is



px <- matrix(nrow = length(lon), ncol = length(lat))





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424560%2funable-to-store-data-in-a-matrix%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You have just to make a small fix on the matrix px declaration. Now you set the number of rows and columns as vectors: nrow = 1:length(lon) and nrow = 1:length(lon). R silently takes only the first elements of these vectors and generates a 1 to 1 matrix. (Actually, it would generate a warning, by the warnings are supressed!)



    So, the solution is



    px <- matrix(nrow = length(lon), ncol = length(lat))





    share|improve this answer




























      0














      You have just to make a small fix on the matrix px declaration. Now you set the number of rows and columns as vectors: nrow = 1:length(lon) and nrow = 1:length(lon). R silently takes only the first elements of these vectors and generates a 1 to 1 matrix. (Actually, it would generate a warning, by the warnings are supressed!)



      So, the solution is



      px <- matrix(nrow = length(lon), ncol = length(lat))





      share|improve this answer


























        0












        0








        0







        You have just to make a small fix on the matrix px declaration. Now you set the number of rows and columns as vectors: nrow = 1:length(lon) and nrow = 1:length(lon). R silently takes only the first elements of these vectors and generates a 1 to 1 matrix. (Actually, it would generate a warning, by the warnings are supressed!)



        So, the solution is



        px <- matrix(nrow = length(lon), ncol = length(lat))





        share|improve this answer













        You have just to make a small fix on the matrix px declaration. Now you set the number of rows and columns as vectors: nrow = 1:length(lon) and nrow = 1:length(lon). R silently takes only the first elements of these vectors and generates a 1 to 1 matrix. (Actually, it would generate a warning, by the warnings are supressed!)



        So, the solution is



        px <- matrix(nrow = length(lon), ncol = length(lat))






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 '18 at 13:01









        EkatefEkatef

        72149




        72149






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424560%2funable-to-store-data-in-a-matrix%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            TypeError: fit_transform() missing 1 required positional argument: 'X'