For loop in R through data.frame - r

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

Related

Can I use a variable to identify a column name in an R dataframe?

Let's say I have a df with many columns and 10 of those columns are named "date1" to "date10"
I want to do something like:
for (x in 1:total_number_of_rows) {
for (y in 1:10) {
column_variable_name <- paste0("date",y)
if (df$column_variable_name[x] <= df$another_date_column[x]) {
*lots more code here* }
}
}
Right now what I'm doing is:
for (x in 1:total_number_of_rows) {
if (df$date1[x] <= df$another_date_column[x]) {
*lots more code here* }
if (df$date2[x] <= df$another_date_column[x]) {
*lots more code here* }
if (df$date3[x] <= df$another_date_column[x]) {
*lots more code here* }
if (df$date4[x] <= df$another_date_column[x]) {
*lots more code here* }
if (df$date5[x] <= df$another_date_column[x]) {
*lots more code here* }
etc...
}
And the "lots more code here" is all the same code each time.
The goal is to not have to copy and paste the code 10 times just because I need to change the variable name in the if statement. The first set of code above doesn't work because it's looking for a column in the dataframe called "column_variable_name". Is there any way to do this? What I'm doing in the second set of code seems unnecessary.
If we need to identify the fruit columns having all values less than or equal to a particular column
nm1 <- startsWith(df, "fruit")
nm1[!colSums(df[nm1] > df[["anothervariable"]], na.rm = TRUE)]
Or another option is Reduce
nm1[Reduce(`|`, lapply(df[nm1], `>=`, df[["anothervariable"]]))]

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.

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?

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