我想根据if
或ifelse
语句的结果运行多行代码。测试表达式将测试数据框中列的值。这是我的问题:
if
语句只允许在测试表达式不是向量时执行多个语句:
if (test_expression) {
statement1
statement2
statement3
}
ifelse
语句允许在测试表达式为向量时执行一个语句:
ifelse(test_expression, statement1, statement2)
但是,如果要在向量上运行if
语句然后根据结果运行整段代码,该怎么办?在这个例子中,我想要运行的代码行将绘制一个图。但实际上,我想要比这更进一步(将生成的图作为电子邮件附件发送)。
示例数据:
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),]
示例if
声明:
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$c, mydf2$elev, xaxt='n', yaxt='n', ylim = c(216,285), xlim= c(0,35), col="mediumpurple1")
points(mydf2$c, mydf2$elev, xaxt='n', yaxt='n', ylim = c(216,285), xlim= c(0,35), pch=21, col="black", bg= "mediumpurple1")
dev.off()
}
答案 0 :(得分:1)
如果你想在向量的某个值满足条件时执行一个代码块,那么 var model = _mapper.Map<ThingModel>(
//realthing,
opt =>
{
opt.Items.Clear();
mappingOptions.Aggregate(
opt.Items,
(items, option) =>
{
items.Add(option.Key, option.Value);
return items;
}
);
}
);
如果任何值为TRUE则返回true。
您的if语句变为:any
以前的答案也可能有用:
我会将你的if条件设置为逻辑向量:
if (any(mydf2$c > 2))
然后在子集数据上调用您的块:
lv <- 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$c[lv], mydf2$elev[lv], xaxt='n', yaxt='n', ylim = c(216,285), xlim= c(0,35), col="mediumpurple1")
points(mydf2$c[lv], mydf2$elev[lv], xaxt='n', yaxt='n', ylim = c(216,285), xlim= c(0,35), pch=21, col="black", bg= "mediumpurple1")
dev.off()
并发出警告:
mydf2$c