Compare dimension of two matrices in R - r

Having two matrices, x and y, how may I check if their dimensions match?
I though about comparisons like
if(nrow(x) == nrow(y) && ncol(x) == ncol(y)) { ... }
or
if(min(dim(x) == dim(y)) == 1) { ... }
but this doesn't seem quite straigth forward.
Question: Is there a single function / single command for matrices to check if they have the same dimension (something like sameDim(x,y))?

As suggested by #eipi10, I now use identical(dim(x), dim(y)).

Related

How to implement a function with a sum inside in R?

I am trying to define a function with a for loop and inside a conditional in R studio. Yesterday I was able with the help of another thread to devise this piece of code. The problem is that I want to sum the vector elements ma for any possible x, so that is inside the function l. This is a simpler case which I am trying to solve to adapt the original model. However, I do not know how to proceed.
ma<-rep(0,20)
l <- function(x, ma) {
for(i in seq_along(ma)) {
if(i %% 2 == 1) {
ma[i] <- i + x
} else {
ma[i] <- 0
}
}
return(ma)
}
My problem is that I would like to have the sum of i+x+0+i+x... for any possible x. I mean a function of the kind for any possible x.
Question:
Can someone explain to me how to implement such a function in R?
Thanks in advance!
I am going to update the original function:
Theta_alpha_s<-function(s,alpha,t,Basis){
for (i in seq_along(Basis)){
if(i%% 2==1) {Basis[i]=s*i^{-alpha-0.5}*sqrt(2)*cos(2*pi*i*t)}
else{Basis[i]=s*i^{-alpha-0.5}*sqrt(2)*sin(2*pi*i*t)}
}
return(Basis)
}
If you don't want to change the values in Basis, you can create a new vector in the function (here result) that you will return:
l = function(s,alpha,t,Basis){
is.odd = which(Basis %% 2 == 1)
not.odd = which(Basis %% 2 == 0)
result = rep(NA, length(Basis))
result[is.odd] = s*is.odd^{-alpha-0.5}*sqrt(2)*cos(2*pi*is.odd*t)
result[not.odd] = s*not.odd^{-alpha-0.5}*sqrt(2)*sin(2*pi*not.odd*t)
#return(result)
return(c(sum(result[is.odd]), sum(result[not.odd])))
}

R function length error message

I made a function to to compute the sum of I(Xi
my.ecdf<- function(x,y) {
if(!is.null(dim(y)))
stop("y has more than one dimension")
n<-length(x)
i<-1:n
p<-if(x[i]<y) 1 else {
0
}
(sum(p))/n
}
But when I run it with input (rnorm(11),6), I get this error:
Warning message:
In if (x[i] < y) 1 else { :
the condition has length > 1 and only the first element will be used
Any ideas? I'm new to r so sorry if it's something obvious. (Also I don't want to use the for loop)
There are a number of issues in your code:
1) Whats the point of x[1:length(x)] in the if statement? Right now these are meaningless and can be dropped:
n<-length(x)
i<-1:n
x[i]
2) If statement accepts a logical argument not a vector of logical, you can consider adding all() any() etc like
if(all(x < y)) 1 else {0}
or use ifelse() statement for the assignment
3) Finally from what I can understand you overcomplicate things and the whole thing can be written as one-liner:
sum(x < y)/length(x)
This is a logical vector of the same length as y
is.null(dim(y))
You're using it as a logical test. An object with a length greater than 1 can't be unambiguously interpreted by the if statement. Consider if (TRUE FALSE FALSE TRUE) <do something>. When should you do that thing?
If you want to make sure y doesn't have more than one dimension, do
if(length(dim(y)) > 1){
stop("message")
}

Divide&Conquer Algorithm for Checking if a list is sorted in Descending order

I need to write a function that checks if an array/list/vector(in R)/
is sorted in Descending order. The answer must be implemented using Divide&Conquer.
Here is what I tried(The Implementation is in R):
is.sort<-function(x){
n<-length(x)
if (n==1)
return(T)
mid<-floor(n/2)
x1<-is.sort(x[1:mid])
x2<-is.sort(x[(mid+1):n])
return(ifelse(x1>=x2,T,F))
}
But thats always returns T :/
Thanks for the Help
It is quite awkward to implement that using D&C. However, let's try.
A sequence of length 1 is always sorted.
If x is of length >= 2, then divide it into 2 non-empty subsequences x1 and x2. Then x is sorted if x1 is sorted, x2 is sorted, and the last element in x1 is greater than or equal to (assuming a non-increasing ordering) the first element in x2.
This may be implemented as:
is.sort<-function(x){
n<-length(x)
if (n==1)
return(TRUE)
mid<-floor(n/2)
return(x[mid] >= x[mid+1] && is.sort(x[1:mid]) && is.sort(x[(mid+1):n]))
}
BTW, R has a is.unsorted function, but it determines only if a sequence is sorted w.r.t. < or <=. For those who miss its >= equivalent here's a trivial Rcpp implementation:
Rcpp::cppFunction('
bool is_sort(NumericVector x) {
int n=x.size();
for (int i=0; i<n-1; ++i)
if (x[i] < x[i+1]) return false;
return true;
}
')

Comparing two values in R

I am checking the input of a matrix is reciprocal in R, i.e. value on one side is = to 1/value..
So far I have..
AHP <- function(pairwisematrix){
## check the input data
if (!((is.matrix(pairwisematrix) || (is.data.frame(pairwisematrix)))))
stop("pairwise matrix must be a matrix or a data frame")
if (!(nrow(pairwisematrix) == ncol(pairwisematrix)))
stop("pairwise matrix must be a square matrix or a data frame")
for (i in 1:nrow(pairwisematrix)) {
for (j in 1:ncol(pairwisematrix)) {
if (i == j) { if (pairwisematrix[i,j] != 1) { stop("the central values in the reciprocal matrix must be 1.") }}
else { if ((pairwisematrix[i,j] == 1 / pairwisematrix[j,i]) || (pairwisematrix[j,i] == 1 / pairwisematrix[i,j])))) { stop("the pairwise matrix must be reciprocal (i.e. value on one side must = 1/value)") }}
}
}
out <- "all worked"
return(out)
}
but when I test:
check1 <- matrix(c(1,1/3,5,3,1,0.2,0.2,5,1),3,3,byrow=T)
test <- AHP(check1)
I get the error:
the pairwise matrix must be reciprocal (i.e. values on one side must =
1/value)0.333333 & 0.333333
I have tried converting the values to string, partial strings and tried identical(a,b,) with no success.
Does anyone have any ideas?
With many if s inside double for loops, I would be surprised if it works as intended.
R is designed for working with matrix, so you could write something like
AHP <- function(pairwisematrix){
if(!all(pairwisematrix == t(1/pairwisematrix)))
stop("the pairwise matrix must be reciprocal (i.e. value on one side must = 1/value)")
else
return("all worked")
}
AHP(check1)
#[1] "all worked"

How to get equivalent to R ifelse functionality, using logical operators

Please can anyone advise how I can turn the following statement into one that will do the same thing but NOT using ifelse please?
<-ifelse(y>=50, 0.2*x+0.8*y, ifelse(y<50 & x>70, y+10, ifelse(y<50 & x<70, y)))
x=80
y=60
So I the final code should give an answer of 64 - selecting the first condition. I will then test it to ensure the other 3 conditions give the correct result for varying values of x and y
Thanks a lot.
This should work:
finalmark <- (x * 0.2 + y * 0.8) * (y >= 50) + (y + 10 * (x > 70)) * (y < 50)
Something like this?
if(y>=50){
0.2*x+0.8*y
}else{
if(y<50 & x>70){
y+10
}else{
if(y<50 & x<70){
y
}else{
"OMG I did not expect this scenario"
}
}
}
try: y=45; x=70 to see why I have the last condition.
If y is a number then, once you've tested for y > = 50 then y must be less than 50 so don't keep testing for that. Similarly, once you've found x > 70 then you don't need the last ifelse. You don't have a return for x = 70. My guess is that you want to test for a <= or >= situation there.
ifelse(y>=50, 0.2*x+0.8*y, ifelse(x>70, y+10, y))
in scalar that's
if(y >= 50){
0.2*x+0.8*y
}else if(x > 70){
y+10
}else y
Given you seem to be having a hard time in general writing the logic I suggest you post a more complete question. It's possible (probable) that you're doing something here that you really don't want to do.
There are several approaches you can take. Below are a few examples of building a function 'f', so that 'f(x,y)' meets your criteria listed in the question using logic other than 'ifelse' statements.
Note: I'm also adding in one amendment to the original post, since 'x=70' would break the logic. I'm adding 'x>=70' to the second criterion.
Option 1: Use a standard 'if / else if / else' logic block. Personally, I like this option, because it's easily readable.
f <- function(x, y){
if (y>= 50){
return(0.2*x+0.8*y)
} else if (y < 50 & x >= 70){
return(y+10)
} else {
return(y)
}
}
Option 2: Combine your two logical tests (there are really only two) into a string, and use a switch. Note that the final and unnamed option is treated as an 'else'.
f <- function(x, y){
return(
switch(paste(x >= 70, y >= 50, sep=""),
TRUEFALSE = y + 10,
FALSEFALSE = y,
0.2*x+0.8*y
)
)
}
Option 3: Order your 'if' statements to reduce logical comparisons. This is the sort of thing to do if you have a large data set or very limited memory. This is slightly harder to troubleshoot, since you have to read the whole block to fully understand it. Option 1 is better if you don't have memory or cycle limitations.
f <- function(x, y){
if (y >= 50){
return(0.2*x+0.8*y)
} else {
if (x >=70){
return(y+10)
} else {
return(y)
}
}
}
There are other options, but these are the simplest that come readily to mind.

Resources