Unable to store data in a matrix
up vote
1
down vote
favorite
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
add a comment |
up vote
1
down vote
favorite
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
This just means that somewhere, in one of thepx
a number is going in that doesn't correspond to anything inpx
. 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 at 7:18
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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
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
r loops
edited Nov 22 at 16:29
Ekatef
65549
65549
asked Nov 22 at 5:44
piyush bhardwaj
194
194
This just means that somewhere, in one of thepx
a number is going in that doesn't correspond to anything inpx
. 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 at 7:18
add a comment |
This just means that somewhere, in one of thepx
a number is going in that doesn't correspond to anything inpx
. 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 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 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 at 7:18
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
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))
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
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))
add a comment |
up vote
0
down vote
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))
add a comment |
up vote
0
down vote
up vote
0
down vote
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))
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))
answered Nov 22 at 13:01
Ekatef
65549
65549
add a comment |
add a comment |
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
This just means that somewhere, in one of the
px
a number is going in that doesn't correspond to anything inpx
. 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 at 7:18