R, problems using a for cycle inside if else - r

I am trying to check if all the elements inside a vector are the same. I am using this code:
if( isTRUE(for(i in 1:length(x)){x[1]==x[i]})){print(x[1])} else{print("several")
Now suppose
x <- c(0,0,0,0,0,0,0,0,0,0,0)
Here, the code should return "0" and if
x <- c(0,0,0,0,0,1,0,0,0,0,0)
it should return "several". In both cases I get "several", any idea why is not working as desired?
Thank u in advance.

there is a simpler way:
if (length(unique(x)) == 1) {
print(x[1])
} else {
print("several")
}
If you want to compare all components of x with the first component you should use all instead of a for loop:
if (all(x == x[1])) {
print(x[1])
} else {
print("several")
}
The result of both approaches is the same.

Related

If else statement to check if any numbers are negative in R

This is probably very simple, but I am not sure why it's not working.
For input vector b, I want to write a function which begins by checking b for any negative values. If there are any, then the function stops. Otherwise, it continues. What the function is doesn't matter.
Something like this:
F <- function(b) {
if (any(b) < 0) {
warning("error")
} else {
# the function I want to put in
}
}
Edit:
The code that works is
F <- function(b) {
if (any(b < 0)) {
stop("error")
} else {
# the function I want to put in
}
}

If-else statement inside apply function R

I am writing an apply function in R to search a table and return all the instances that TRUE occurs, and I have written the following code but it keeps giving me errors, and I am not sure why. Any help is appreciated.
xsum = apply(genomeTable, 1, function(i) {
if (i) < q.start | if (i) > q.end{
return FALSE
} else{
return TRUE
}
})
sum(xsum)
Your if statement is duplicated. You only need one if for the condition. Parenthesis need to wrap the whole condition. Try this:
xsum = apply(genomeTable, 1, function(i) {
if (i < q.start | i > q.end) {
return(FALSE)
} else {
return(TRUE)
}
})
Try this:
xsum = apply(genomeTable, 1, function(i) ifelse ((i < q.sta | i > q.end), FALSE, TRUE))
It does not work, please provide some data.

Putting a condition in a for loop

Let's say
i = 1:2029
j = c(3,6,3,5,123,323,423,652,743,885,932)
for loop generally looks like
for (x in i) { }
in here, I would like to calculate the function for x that are not same as j
so it would be something like
for (x in i & !j) { }
but obviously, this kind of command doesn't work.
Is there are command that expresses my intention?
You can use :
for (x in i[-which(i %in% j)]) { }

For loop in R through data.frame

I have the following data.frame
Name<-c("Jack","Jerry","Emma","Andy","Jayde","Lynn","Liam")
Education<-c("Master","Master","Master","Bach","Bach","PhD","PhD")
Salary<-c(20000,20000,20000,30000,10000,70000,70000)
People<-data.frame(Name,Education,Salary)
I now have to use a for loop (silly, I know) that will loop through the frame, find the "education" level, and add a salary increase.
how can this be done?
Even though it would be better to do as mentioned in the comments, you can do it the ugly way:
for (i in 1:nrow(People)) {
if (People$Education[i] == "Bach") {
People$Salary[i] <- People$Salary[i]+1000
} else if (People$Education[i] == "Master") {
People$Salary[i] <- People$Salary[i]+2000
} else {
People$Salary[i] <- People$Salary[i]+3000
}
}

Is there a "functional if" in R?

Basically I'm looking for an equivalent of
for (i in 1:nrow(mydata)) {
if(mydata$alive[i]) { mydata$result[i] = mydata$alive_value; }
else { mydata$result[i] = mydata$dead_value; }
}
That would be along the lines of
mydata$result <- func_if(mydata$alive,mydata$alive_value,mydata$dead_value)
Does something like that exist?
You're looking for ifelse. Documentation: http://stat.ethz.ch/R-manual/R-devel/library/base/html/ifelse.html.
mydata$result <- ifelse(mydata$alive, mydata$alive_value, mydata$dead_value)

Resources