Is there a "functional if" in R? - 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)

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
}
}

R, problems using a for cycle inside if else

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.

Data table filtering does not work

I'm trying to create a data table and do some calculations about it for my assignment. However, when I create and manipulate data, for some reason filtering does not work. For instance if I filter column 's' for the value 0.7, no solution. For the value 0.9, it works as it should. It's weird.
Thanks for any help.
library(data.table)
p<-as.data.table(cbind(0:6, c(0,0.15,0.33,0.37,0.40,0.42,0.43)))
states<-seq(from=0, to=30,by=0.1)
actions<-seq(from=0,to=6)
actions<-as.data.table(actions)
actions[,hunt_share:=numeric()]
for(i in 1:7)
{
if(i==1)
actions[i]$hunt_share<-0
else
actions[i]$hunt_share<-floor(164/(i-1)*10)/10
}
u_star<-data.table(t=integer(),s=numeric(),a=integer(),value=numeric())
r<-data.table(t=integer(),s_bar=numeric(),s=numeric(),a=integer(),value=numeric())
trans_prob<-data.table(t=integer(),s_bar=numeric(),s=numeric(),a=integer(),value=numeric())
str(trans_prob)
####### transitional probabilities
horizon<-5
for(time in 1:horizon)
{
for(i in states)
{
for(a in actions$actions)
{
if((a==0))
{
tmp<-data.table(t(c(time,max(i-6,0),i,a,1)))
colnames(tmp)<-colnames(trans_prob)
trans_prob<-rbind(trans_prob,tmp,fill=T)
}
if((a>0)&(i>=0.5))
{
tmp<-data.table(t(c(time,min(30,i+actions[actions==a]$hunt_share-6.5),i,a,p[V1==a]$V2)))
colnames(tmp)<-colnames(trans_prob)
trans_prob<-rbind(trans_prob,tmp,fill=T)
tmp<-data.table(t(c(time,max(i-6.5,0),i,a,1-p[V1==a]$V2)))
colnames(tmp)<-colnames(trans_prob)
trans_prob<-rbind(trans_prob,tmp,fill=T)
}
}
}
print(time)
}
####### transitional probabilities
##Bug?
trans_prob[s==0.9]
trans_prob[s==0.7]
##Bug?

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
}
}

Resources