ifelse, which, %in% are extremely important functions in R language and will often be seen in other programs in the future.
ifelse
ifelse is the abbreviation of if condition judgment statement, its usage is as follows:
ifelse(test,yes,no)
parameter | describe |
---|---|
test | A logical expression that can be judged |
yes | The object returned after being judged as true |
no | The object returned after being judged as flase |
For example:
x = 5 ifelse(x,1,0)
If x is not equal to 0, it returns 1, and if it is equal to 0, it returns 0.
which
which returns a handle with the condition true, and returns its index to the correct logical object.
which(test,=FALSE)
test must be a logical object, a logical array.
For example:
which(LETTERS == "R")
%in%
%in% determines whether the previous object is in the subsequent container
element %in% list veator
1 %in% c(1:3)
Supplementary: R language: if-else condition judgment and any, all, usage method
Basic structure display:
if (7<10) { print("Seven is less than ten") } else{ print("seven is more than ten") }
Example demonstration:
Titanic=("/4Gqsnz") #Read data from the network
1. any()
#any means that as long as any value meets it, it is TRUEif (any(titanicC$Age>70)) { print("there are passengers older than 70") } else{ print("no one is older than 70") }
2. all()
#It is true if everything is satisfiedif (all(titanicC$Age>10)) { print("all passengers older than 10") } else{ print("there are passengers younger than 10") }
3. ()
#The position of the placement determines whether to delete the missing value of a single variable or delete any missing value of a variableif (any((titanic$Age==100))) { print("there are passengers aged 100") } else{ print("there are no passengers aged 100") } #Any missing records in the database are deletedif (any(titanic$Age==80, =TRUE)) { print("there are passengers aged 80") } else{ print("there are no passengers aged 80") } #Age This variable has missing records deleted, and other variables can be retained.
4. else if write more repetitive statements
x=100 y=10 if(x<y){ print("AA") } else if(x==y){ print(BB) } else{ print(CC) }
The above is personal experience. I hope you can give you a reference and I hope you can support me more. If there are any mistakes or no complete considerations, I would like to give you advice.