I want to run multiple lines of code based on the results of an if or ifelse statement. The test expression will test the values of a column in a dataframe. Here is my problem:
if statements allow multiple statements to be executed only when the test expression is not a vector:
if (test_expression) {
statement1
statement2
statement3
}
ifelse statements allow one statement to be executed when the test expression is a vector:
`ifelse`(test_expression, statement1, statement2)
However, what if you want to run an if statement on a vector and then run a whole section of code based on the results? In this example, the lines of code I want to run will make a plot. But in reality, I want to go much further than this (send the generated plot as an email attachment).
Example Data:
datetime <- c("12/6/2016 8:00","12/6/2016 8:00","12/6/2016 8:00","12/6/2016 8:00","12/6/2016 8:00","12/6/2016 8:00","12/6/2016 8:00","12/6/2016 8:00","12/6/2016 8:00","12/6/2016 8:00","12/6/2016 10:00","12/6/2016 10:00","12/6/2016 10:00","12/6/2016 10:00","12/6/2016 10:00","12/6/2016 10:00","12/6/2016 10:00","12/6/2016 10:00","12/6/201610:00","12/6/2016 10:00","12/6/2016 10:00","12/6/2016 10:00","12/6/2016 10:00","12/6/2016 10:00","12/6/2016 10:00","12/6/2016 12:00","12/6/2016 12:00","12/6/201612:00","12/6/2016 12:00","12/6/2016 12:00","12/6/2016 12:00","12/6/2016 12:00","12/6/2016 12:00","12/6/2016 12:00","12/6/2016 12:00","12/6/2016 12:00","12/6/201612:00","12/6/2016 12:00","12/6/2016 12:00","12/6/2016 12:00")
c <- c(2.41,1.68,2.29,2.09,3.47,2.28,2.56,2.52,2.27,1.74,2.03,2.14,2.77,2.34,1.78,2.53,2.68,2.27,1.83,1.69,1.83,2.3,2.07,1.91,2.16,3.11,2.38,2.63,2.47,2,2.35,2.11,2.03,3.2,2.17,2.58,2.64,2.23,2.12,2.17)
elev <- c(239.5312,242.8121,246.257,249.5378,252.6874,255.7714,259.2819,262.3331,265.8436,266.8278,222.6283,226.3685,229.7806,232.9302,236.211,239.5247,242.8055,245.9223,249.3344,252.7465,256.0601,259.1441,262.5234,265.9026,266.7885,222.6513,226.3915,229.8692,232.9531,236.2012,239.6133,242.8613,246.2406,249.4558,252.7694,256.0175,259.3639,262.6448,265.86,266.8442)
mydf <- data.frame(datetime,c,elev)
mydf$datetime <- strptime(mydf$datetime, format = "%m/%d/%Y %H:%M")
current.time <- "2016-12-06 12:00:00"
current.time <- paste(format(current.time, format = "%Y-%m-%d %H"), ":00:00", sep = "")
current.date <- "2016-12-06"
mydf2 <- subset(mydf, datetime == current.time)
mydf2 <- mydf2[order(-mydf2$elev),]
Example if statement:
if(mydf2$c > 2){
filename<- paste("//C:/Alert_Profile_",current.date, ".pdf", sep="")
pdf(filename, width=7, height=12)
plot(0,type="n", ylim = c(216,285), xlim= c(0,35),ylab="Elevation (ft ASL)", xlab=expression('A ('*degree*'C)'))
lines(mydf2$a, mydf2$elev, xaxt='n', yaxt='n', ylim = c(216,285), xlim= c(0,35), col="mediumpurple1")
points(mydf2$a, mydf2$elev, xaxt='n', yaxt='n', ylim = c(216,285), xlim= c(0,35), pch=21, col="black", bg= "mediumpurple1")
dev.off()
}
Aucun commentaire:
Enregistrer un commentaire