Language learning function R 4 Notes

Conditional statements and loops

When you use the time to talk if, at this time the condition appeared in
the example cited conditions function

sign_t<-function(x){
  if(x>0){
    return(1)
  }else if(x<0){
    return(-1)
  }else{
    return(0)
  }
}            
sign_t(5)            
#[1] 1

This is a simple question, complex problems, not put the demand to write it down, but that is the implementation of computer language fool you going to do, could have been used to reduce repetitive operation, languages ​​are the same, the stars you have to refuel myself, the before never learned to make it up

Generalization of structure

if (condition1){
   express1
} else if (condition2){
   express2
} else if (condition3){
   express3
}...
}else (condition final){
  express final
}
}

x write a score of small demo

> grade<-function(name,score){
+   if (score<70){
+     return("D")
+   }else if (score>=70&&score<80){
+     return("C")
+   }else if (score>=80&&score<90){
+     return("B")
+   }else {
+     return("A")
+   }
+ }
>  grade(89)
[1] "B"
> 

另一个小demo
namegrade<-function(name,score){
+ grade<-
+   if (score<70) "D"
+   else if (score>=70&&score<80) "C"
+     
+   else if (score>=80&&score<90)  "B"
+     
+   else "A"
+   
+   cat("the grade of",name,"is",grade)
+ }
>  namegrade("jhon",89)
the grade of jhon is B

再来一个
gradename<-function(name,score){
   if (score<70){
     grade<-"D"
     cat("what a pity!\n")
   }else if (score>=70&&score<80){
     grade<-"C"
   }else if (score>=80&&score<90) {
     grade<-"B"
   }else{
     grade<-"A"
     cat("good\n")
   }
   cat("the grade of",name,"is",grade)
 }
 gradename("xing",90)
##good
the grade of xing is A

Because of the nature if the original is a function, its return value is a value that satisfies the condition expression of the branch, so the expression can be used as inline

 sign_1<-function(x){
+    if (x>0) 1 else if (x<0) -1 else 0
+  }
>  sign_1(5)
[1] 1
> 

ifelse

This function takes a logical vector as the determination condition, and returns a vector for the logic decision for each element in a condition, if true, then selects the second parameter yes in the corresponding element, if FALSE
, then selects the third parameter no corresponding element of
small demo

  ifelse(c(TRUE,FALSE,FALSE),c(1,2,3),c(4,5,6))
[1] 1 5 6

switch slightly

Loop expression

The official explanation:
loop (or iteration) by iterating a vector (for) or check whether a condition is violated (while) to repeat the implementation of an expression
, if only to make some changes in the input value, and to repeat the same task purpose is to reduce the redundant code

Use a for loop

for circulating a given vector or list, repeat the implementation of an expression through an iterative

Syntax

R & lt} { `` `
for (var in vector) {
expr
}
value var traverse each element in the vector, expr, iteration is performed, if there are n elements in the vector, this expression is equivalent to that
var <- Vector [[. 1]]
expr
var <-vector [[2]]
expr
var <-vector [[. 3]]
expr

To a small demo

>  for (i in 1:3){
+    cat("the value of i is ",i,"\n")
+  }
the value of i is  1 
the value of i is  2 
the value of i is  3 

Of course, iterations can be applied to all types of vectors, not only numerical vector

for (word in c("hello","new","world")){
   cat("the word is",word,"\n")
 }

Or a list of iterative

>  loop_list<-list(
+    a=c(1,2,3),
+    b=c("a","b","c","d"))
>  for (item in loop_list){
+    cat("item:\n lenhth:",length(item),"\n class:",class(item),"\n")
+  }
item:
 lenhth: 3 
 class: numeric 
item:
 lenhth: 4 
 class: character

In the iteration when a data frame can be viewed as a list, but note that the data frame columns (elements) to be consistent length, then the default iteration is performed in accordance with the column
to a small demo

>  df<-data.frame(
+    x=c(1,2,3),
+    y=c("a","b","c"),
+    stringsAsFactors = FALSE)
>  for (col in df){
+    str(col)
+  }
 num [1:3] 1 2 3
 chr [1:3] "a" "b" "c"

Back can learn about the apply function, map, than for Haoshi

Sometimes variables can be used to record an outer loop iteration state or accumulation process, such as the 100 and seek

> s<-0
> for(i in 1:100){
+   s<-s+i
+ }
> s
[1] 5050

Simple simulation about the process of random walk

> set.seed(123)
> x<-numeric(1000)
> for (t in 1:(length(x)-1)){
+   x[[t+1]]<-x[[t]]+rnorm(1,0,0.1)
+ }
> plot(x,type="s",main="Random walk",xlab="t")

Sometimes be interrupted for loop

> for (i in 1:5){
+   if(i==3) break
+   cat("message",i,"\n")
+ }
message 1 
message 2 

Further functions can use the rest of this next iteration skips

> for (i in 1:5){
+   if(i==3) next
+   cat("message",i,"\n")
+ }
message 1 
message 2 
message 4 
message 5 

Nested loop

Both the for loop sets together, common applications, multiplication tables

> for(i in 1:9){
+   for(j in 1:9){
+     cat("i*j=",i*j,"\n")
+   }
+   
+ }
i*j= 1 
i*j= 2 
i*j= 3 
i*j= 4 
i*j= 5 
i*j= 6 
i*j= 7 
i*j= 8 
i*j= 9 
i*j= 2 
i*j= 4 
i*j= 6 
i*j= 8 
i*j= 10 
i*j= 12 
i*j= 14 
i*j= 16 
i*j= 18 
i*j= 3 
i*j= 6 
i*j= 9 
i*j= 12 
i*j= 15 
i*j= 18 
i*j= 21 
i*j= 24 
i*j= 27 
i*j= 4 
i*j= 8 
i*j= 12 
i*j= 16 
i*j= 20 
i*j= 24 
i*j= 28 
i*j= 32 
i*j= 36 
i*j= 5 
i*j= 10 
i*j= 15 
i*j= 20 
i*j= 25 
i*j= 30 
i*j= 35 
i*j= 40 
i*j= 45 
i*j= 6 
i*j= 12 
i*j= 18 
i*j= 24 
i*j= 30 
i*j= 36 
i*j= 42 
i*j= 48 
i*j= 54 
i*j= 7 
i*j= 14 
i*j= 21 
i*j= 28 
i*j= 35 
i*j= 42 
i*j= 49 
i*j= 56 
i*j= 63 
i*j= 8 
i*j= 16 
i*j= 24 
i*j= 32 
i*j= 40 
i*j= 48 
i*j= 56 
i*j= 64 
i*j= 72 
i*j= 9 
i*j= 18 
i*j= 27 
i*j= 36 
i*j= 45 
i*j= 54 
i*j= 63 
i*j= 72 
i*j= 81 
咋这么奇怪???

Although the for loop is useful, but it should give priority to built-in functions, such as lapply etc.

a little while loop

Guess you like

Origin www.cnblogs.com/gaowenxingxing/p/11949849.html