【发布时间】:2021-03-04 08:44:48
【问题描述】:
我的 if else 函数需要一些帮助。我希望 R 为我的列 (DipE) 中的每个 raw 打印在我的 if else 函数中定义的相应值。
这是我的专栏:
| TierLID | DipE |
|---|---|
| MLS289 | -4.0020 |
| MLS440 | -3.9680 |
| MLS382 | -3.9280 |
| MLS414 | -3.9280 |
| MLS290 | -2.8480 |
| MLS175 | -2.5210 |
| MLS232 | -2.5020 |
| MLS241 | -2.5020 |
| MLS189 | -2.4040 |
| MLS563 | -2.4040 |
| MLS209 | -2.4000 |
| MLS277 | -0.7396 |
| MLS514 | -0.5619 |
| MLS539 | -0.4518 |
| MLS540 | -0.4518 |
| MLS200 | -0.3709 |
| MLS340 | -0.2090 |
这是我的代码:
if(Sheeps_with_Haplotyp10$DipE >-4 & <-3.5){
print("1")
}else if (Sheeps_with_Haplotyp10$DipE >-3.5 & <-3.0){
print("3")
}else if (Sheeps_with_Haplotyp10$DipE >-3.0 & <-2.5){
print("0")
}else if (Sheeps_with_Haplotyp10$DipE >-2.5 & <-2.0){
print("4")
}else if (Sheeps_with_Haplotyp10$DipE >-2.0 & <-1.5){
print("3")
}else if (Sheeps_with_Haplotyp10$DipE >-1.5 & <-1.0){
print("0")
}else if (Sheeps_with_Haplotyp10$DipE >-1.0 & <-0.5){
print("2")
}else if (Sheeps_with_Haplotyp10$DipE >-0.5 & <-0.0){
print("4")
}
它只打印 4 个
【问题讨论】:
-
试试
Sheeps_with_Haplotyp10$DipE >-3.5 & Sheeps_with_Haplotyp10$DipE <-3.0 -
这能回答你的问题吗? How does cut with breaks work in R
-
注意:
<-是赋值运算符。 -
如果您使用的是
if/else,则需要循环遍历每一行。您可以使用ifelse/cut/findInterval作为矢量化选项。请参阅这些示例 stackoverflow.com/questions/4126326/… 和 stackoverflow.com/questions/12979456/…
标签: r if-statement