Tuesday, 17 September 2013

Creating if + & statement

Creating if + & statement

I'm using R and need to create code that checks if a matrix dimensions has
3x3. I am stuck on the proper code using if() to check the nrow and ncol.
Any help or suggestion greatly appreciated.
localSmoother <- function(myMatrix = matrix(), smoothingMatrix = matrix(1,
nrow = 3, ncol = 3))
{
if(class(smoothingMatrix) != "matrix") stop ("smoothingMatrix must be a
matrix.")
if(ncol(smoothingMatrix) != "3" & if(nrow(smoothingMatrix) != "3")
stop ("smoothingMatrix must have dimensions of 3x3")
print(myMatrix) # see what myMatrix is.
print(smoothingMatrix) # see what smoothingMatrix is.
return(matrix(, nrow = 3, ncol = 3))
}
# # TEST the CODE:
localSmoother(myMatrix = diag(x = 2, nrow = 5, ncol = 5), smoothingMatrix
= matrix(2, nrow = 5, ncol = 3))
# # Error in localSmoother(myMatrix = diag(x = 2, nrow = 5, ncol = 5),
smoothingMatrix = matrix(2, :
# # smoothingMatrix must by 3x3

No comments:

Post a Comment