Error while using ifelse in apply function - R - r

Hello I have the following code;
valorNegativos = function (vela) {
for(a in 1:length(vela)) {
apply(vela[[a]], 2, function(col) ifelse( length(as.numeric(col) < 0) == length(vela[[a]]$FECHA), print(paste0("Correct data")), stop("ERROR: Negative values"))
}
}
vela, is a list of dataframes.
What I have to do with this function is to prove if all the columns values from all my dataframes are positive ( > 0 ) and to check if the number of values is equal to the value count from one column $FECHA ( date ).
The problem is that I am getting the two posibilites from the ifelse.
[1] "Correct data"
Show Traceback
Rerun with Debug
Error in ifelse(length(as.numeric(col) > 0) == length(vela[[a]]$FECHA, :
ERROR: Negative values
Any idea?

Related

Attempting a for loop in R

x <- 1:19
count <- 0
for (i in x) {
if atranspose * T5_5_FBEETLES[i, 3:6]>cutoff
count=count+1
}
print(count)
Hello, I am trying to do a for loop in R. In this for loop, I am multiplying a 1x4 matrix (atranspose in this case) and the third through sixth columns of a table (the table is T5_5_FBEETLES in this case) row by row (hence the i in x, so going through the first 19 rows) and I'm comparing it to a number with the variable name of cutoff. If the multiplication ends up with something greater than the cutoff number, I want count to increase by 1. I know from doing this by hand that by the end count should be 19, but for whatever reason my for loop returns 1 for my count variable and I keep getting these two errors:
unexpected symbol in:
"for (i in x) {
if atranspose"
unexpected '}' in "}"
Can anyone explain to me why these two errors are occurring, and how I can fix up my for loop so that it can return the correct count?
You are getting an error because your if statement crosses a line and thus needs some curly braces:
x <- 1:19
count <- 0
for (i in x) {
if (atranspose * T5_5_FBEETLES[i, 3:6]>cutoff) {
count=count+1
}
}
print(count)
This will then give you another error because the logical check of the if statement will return a vector, so it needs to be wrapped in an any:
x <- 1:19
count <- 0
for (i in x) {
if (any(atranspose * T5_5_FBEETLES[i, 3:6]>cutoff)) {
count=count+1
}
}
print(count)

max(df$data==v) returns numeric(0) depending on x,y order in v = c(x,y)

I'm trying to select the maximum between 2 numbers (out of 5) using the following code.
These are the two levels of the column I'm trying to access:
lev_opsins_SC = c("SWS2Aa", "SWS2B")
They are repeated for each individual (but that loop works).
For each individual I'm trying to tell R to access only the data which correspond to lev_opsins_SC and tell me which one is the max and then store it.
I find out what is the problem but don't know how to fix it:
max(df$Rate[df$Specimen=="L20" & df$Opsins == lev_opsins_SC])
[1] 0.509928
max(df$Rate[df$Specimen=="L26" & df$Opsins == lev_opsins_SC])
[1] -Inf
Warning message:
In max(df$Rate[df$Specimen == "L26" & df$Opsins == lev_opsins_SC]) :
no non-missing arguments to max; returning -Inf
These are the output for two different individuals L26 and L20.
I found out that if I change the order of my levels like this:
lev_opsins_SC = c("SWS2B", "SWS2Aa")
I get the maximum for L26 and the error message for L20.
Any idea why this is happening?
The data is in a .csv file which has the saved with the exact same level order for all individuals.
Full loop (lev_ind is levels for individuals):
for (i in lev_ind) {
maximum = max(df$Rate[df$Specimen==i & df$Opsins == lev_opsins_SC])
for (j in lev_opsins_SC) {
rate = df$Rate[df$Specimen==i & df$Opsins==j]
if (rate == maximum) {
df$Rate[df$Specimen==i & df$Opsins==j] = 1
} else {
df$Rate[df$Specimen==i & df$Opsins==j] = (1/(maximum))*rate
}
}
}
return(df)
}

argument is of length zero but is.null is false

I'm trying to check a range of numbers against some values in a dataset using for control
for(i in 20:28)
{
for(j in 1:52)
{
if (Test$Ferritin[j]<15 & Test$RHCc[j]<i)
{
Test$Status[j] = "TP"
}
}
}
But I keep getting the error
Error in if (Test$Ferritin[j] < 15 & Test$RHCc[j] < i) { : argument
is of length zero
I did check the condition using is.null, but it returns "False" in the answer.
Can someone explain what I could be doing wrong?
NULL is of length 0, but not all length zero vectors are NULL (you can confirm for yourself via is.null(numeric(0))). Check whether length(argument) == 0 instead.

if is.null or another value R

How would I make this comparison in R?
if (is.null(a)| a == 'LAST') {
# do something
}
If the a is NULL this fails, throwing the error:
Error in if (a == "LAST") { :
argument is of length zero
How could I work around this?

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"

Resources