Statistics with R
- Statistics with R
- R Objects, Numbers, Attributes, Vectors, Coercion
- Matrices, Lists, Factors
- Data Frames in R
- Control Structures in R
- Functions in R
- Data Basics: Compute Summary Statistics in R
- Central Tendency and Spread in R Programming
- Data Basics: Plotting – Charts and Graphs
- Normal Distribution in R
- Skewness of statistical data
- Bernoulli Distribution in R
- Binomial Distribution in R Programming
- Compute Randomly Drawn Negative Binomial Density in R Programming
- Poisson Functions in R Programming
- How to Use the Multinomial Distribution in R
- Beta Distribution in R
- Chi-Square Distribution in R
- Exponential Distribution in R Programming
- Log Normal Distribution in R
- Continuous Uniform Distribution in R
- Understanding the t-distribution in R
- Gamma Distribution in R Programming
- How to Calculate Conditional Probability in R?
- How to Plot a Weibull Distribution in R
- Hypothesis Testing in R Programming
- T-Test in R Programming
- Type I Error in R
- Type II Error in R
- Confidence Intervals in R
- Covariance and Correlation in R
- Covariance Matrix in R
- Pearson Correlation in R
- Normal Probability Plot in R
Control Structures in R
Control structures in R allow you to control the flow of execution of a series of R expressions.
Commonly used control structures are
- if and else: testing a condition and acting on it
- for: execute a loop a fixed number of times
- while: execute a loop while a condition is true
In R, the if/else syntax is:
if (…) {
R code
} else {
R code
}
if-else in R
#Take input from user. Then run the next line.
x <- readline(prompt="Enter a number: ")
y <- if(x > 10){
print("Value is greater than 10")
} else {
print("Value is less than or equal to 10")
}
Output:
> x <- readline(prompt="Enter a number: ") Enter a number: 11 [1] "Value is more than 10"
x = 4
y = 6
if (x > y) {
print("x is larger than y")
} else {
print("x is less than or equal to y")
}
Output:
[1] “x is less than or equal to y”
You can use if-else like this also
ifelse(4 > 3, "greater","less")
Output:
[1] "greater"
This is how we use ifelse() to a vector.
x<- 1:20 ifelse(x> 10, "greater", "less")
Output:
[1] "less" "less" "less" "less" "less" "less" "less" "less" "less" "less" "greater" "greater" [13] "greater" "greater" "greater" "greater" "greater" "greater" "greater" "greater"
for loop in R
Here is a for loop example in R.
for(i in 1:10) {
print(i)
}
x <- c("a", "b", "c", "d")
for(i in 1:4) {
print(x[i])
}
Output:
[1] 1 [1] 2 [1] 3 [1] 4 [1] 5 [1] 6 [1] 7 [1] 8 [1] 9 [1] 10 > > x <- c("a", "b", "c", "d") > for(i in 1:4) { + print(x[i]) + } [1] "a" [1] "b" [1] "c" [1] "d"
Print out each element of ‘x’.
for(letter in x) {
print(letter)
}
Output:
[1] "a" [1] "b" [1] "c" [1] "d"
For one line loops, the curly braces are not strictly necessary.
for(i in 1:4) print(x[i])
Output:
[1] "a" [1] "b" [1] "c" [1] "d"
for loops can be nested inside of each other like the below example.
x <- matrix(1:10, 5, 2)
for(i in seq_len(nrow(x))) {
for(j in seq_len(ncol(x))) {
print(x[i, j])
}
}
Output:
[1] 1 [1] 6 [1] 2 [1] 7 [1] 3 [1] 8 [1] 4 [1] 9 [1] 5 [1] 10
while Loops in R
count <- 0
while(count < 10) {
print(count)
count <- count + 1
}
Output:
[1] 0 [1] 1 [1] 2 [1] 3 [1] 4 [1] 5 [1] 6 [1] 7 [1] 8 [1] 9