Using if/ifelse statement with vector conditions in R
You can also do a normal if statement, since it returns a value:
my_result = if(all(Vector1 == c(T,T,T))) {"Combo1"} else {"Combo2"}
The ifelse function is made for vectorized conditional statements.
By using the standard if statement, you remove potential ambiguity/misinterpretation because the standard if only evaluates one condition.
Vectorized IF statement in R?
x <- seq(0.1,10,0.1)
> x
[1] 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2 1.3 1.4 1.5
[16] 1.6 1.7 1.8 1.9 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.9 3.0
[31] 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 4.0 4.1 4.2 4.3 4.4 4.5
[46] 4.6 4.7 4.8 4.9 5.0 5.1 5.2 5.3 5.4 5.5 5.6 5.7 5.8 5.9 6.0
[61] 6.1 6.2 6.3 6.4 6.5 6.6 6.7 6.8 6.9 7.0 7.1 7.2 7.3 7.4 7.5
[76] 7.6 7.7 7.8 7.9 8.0 8.1 8.2 8.3 8.4 8.5 8.6 8.7 8.8 8.9 9.0
[91] 9.1 9.2 9.3 9.4 9.5 9.6 9.7 9.8 9.9 10.0
> ifelse(x < 5, 1, 2)
[1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
[38] 1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
[75] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2
How to write a if and else statement for a vector?
You might use std::find
:
std::vector<int> myvec {1,2,3,4,5};
if (std::find(myvec.begin(), myvec.end(), 1) != myvec.end()) {
cout << "yes it is there" << endl;
}
R - vectorized if for multiple conditions
How about this?
It's the same function as above, but without the explicit if
and else
?
> Teff=seq(30,40,1)
> hsa<- 1*(Teff<Tcr) + (1 - (Teff - Tcr)/(Tlim - Tcr))*(Teff >= Tcr & Teff < Tlim)
> hsa
[1] 1.0000000 1.0000000 1.0000000 1.0000000 0.8571429 0.7142857 0.5714286 0.4285714 0.2857143 0.1428571 0.0000000
** Note that you can add + 0*(Teff>=Tlim)
in the end, but it wouldn't change anything, because the assigned value would be 0 anyways.
If you really want to use ifelse
, then you have to nest them, so it should be something like this:
> hsa<- ifelse(Teff<Tcr, 1,
ifelse(Teff >= Tcr & Teff < Tlim,
(1 - (Teff - Tcr)/(Tlim - Tcr)), 0))
> hsa
[1] 1.0000000 1.0000000 1.0000000 1.0000000 0.8571429 0.7142857 0.5714286 0.4285714 0.2857143 0.1428571 0.0000000
ifelse function on a vector
ifelse
is vectorized and its result is as long as the test
argument. all(is.na(vect))
is always just length one, hence the result. a regular if/else clause is fine here.
vect <- c("NA_NA", "14_mter", "78_ONHY")
if (all(is.na(vect))) {
out <- vect
} else {
out <- vect[vect != "NA_NA"]
}
out
#> [1] "14_mter" "78_ONHY"
additional note: no need for the which()
here
In R how to use an ifelse() with a vector or dataframe for classification
There is a dplyr
way to do this.
library(dplyr)
sp_data %>%
inner_join(size_data, by = c("X1" = "S1")) %>%
mutate(X4 = case_when(X2 >= S2 ~ "above",
TRUE ~ "below")) %>%
select(-S2)
X1 X2 X4
1 fish1 20 below
2 fish1 30 above
3 fish2 32 above
4 fish2 21 below
5 fish3 50 above
How to search in vector with If and else statement
You're trying too hard, simply
if (myvec[0] == 1)
For extra security you could add a check that the vector is not empty
if (!myvec.empty() && myvec[0] == 1)
Related Topics
Dplyr::Select One Column and Output as Vector
Speeding Up Julia's Poorly Written R Examples
The Difference Between Domc and Doparallel in R
Hyperlinking Text in a Ggplot2 Visualization
Circular Heatmap That Looks Like a Donut
How and When Should I Use On.Exit
R Cmd Check Note: Found No Calls To: 'R_Registerroutines', 'R_Usedynamicsymbols'
Jupyter-Client Has to Be Installed But "Jupyter Kernelspec --Version" Exited with Code 127
Error: --With-Readline=Yes (Default) and Headers/Libs Are Not Available
Writings Functions (Procedures) for Data.Table Objects
Reordering Columns in a Large Dataframe
How to Combine Row and Column Layout in Flexdashboard
To Find Whether a Column Exists in Data Frame or Not
How to Left Align Text in Annotate from Ggplot2
Assign Headers Based on Existing Row in Dataframe in R