MisterTootor M.S., B.S., A.S., A.S.B
I'm a paragraph. Click here to add your own text and edit me. It's easy.
​
​
An R Program to Calculate the Sum of Two Numbers
sum_numbers <- function(a, b) {
result <- a + b # Calculate the sum
return(result) # Return the result
}
# Assign values to two variables
num1 <- 5
num2 <- 10
# Call the sum_numbers function with num1 and num2 as arguments
sum_result <- sum_numbers(num1, num2)
# Print the result
print(paste("The sum of", num1, "and", num2, "is:", sum_result))
Example Output:
[1] "The sum of 5 and 10 is: 15"
An example of descriptive statistics
# Example data: heights of 10 individuals
heights <- c(160, 170, 175, 168, 180, 155, 165, 172, 178, 169)
# Calculate basic statistics
mean_height <- mean(heights) # Mean
median_height <- median(heights) # Median
sd_height <- sd(heights) # Standard deviation
var_height <- var(heights) # Variance
range_height <- range(heights) # Min and Max
# Print results
print(paste("Mean Height:", mean_height))
print(paste("Median Height:", median_height))
print(paste("Standard Deviation:", sd_height))
print(paste("Variance:", var_height))
print(paste("Range of Heights:", range_height[1], "to", range_height[2]))
Output:
[1] "Mean Height: 170.7"
[1] "Median Height: 170"
[1] "Standard Deviation: 7.725188"
[1] "Variance: 59.684"
[1] "Range of Heights: 155 to 180"
An example of Hypothesis Testing: T-test (ChatGPT)
# Perform a one-sample t-test
t_test_result <- t.test(heights, mu = 170)
# Print the t-test result
print(t_test_result)
Output:
One Sample t-test
data: heights
t = 0.073, df = 9, p-value = 0.9436
alternative hypothesis: true mean is not equal to 170
95 percent confidence interval:
164.0531 177.3469
sample estimates:
mean of x
170.7
An example of Regression Analysis
# Example data: x (hours studied) and y (test scores)
x <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
y <- c(55, 57, 60, 62, 64, 67, 69, 72, 74, 75)
# Fit a linear regression model
model <- lm(y ~ x)
# Print the summary of the regression model
summary(model)
Output:
Call:
lm(formula = y ~ x)
Residuals:
Min 1Q Median 3Q Max
-2.67 -1.00 -0.20 0.90 3.40
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 52.4500 1.7075 30.68 2.85e-08 ***
x 2.2500 0.2225 10.12 3.09e-05 ***
Residual standard error: 1.728 on 8 degrees of freedom
Multiple R-squared: 0.927, Adjusted R-squared: 0.919
F-statistic: 102.4 on 1 and 8 DF, p-value: 3.09e-05
An example of Data Visualization - Histogram (ChatGPT)
# Create a histogram of the heights data
hist(heights,
main = "Histogram of Heights",
xlab = "Height (cm)",
col = "skyblue",
border = "black",
breaks = 5)
Output:
The result would be a histogram displaying the distribution of the heights data, with blue bars, labeled axes, and a title.
# take input from the user
num = as.integer(readline(prompt="Enter a number: "))
factorial = 1
# check is the number is negative, positive or zero
if(num < 0) {
print("Sorry, factorial does not exist for negative numbers")
} else if(num == 0) {
print("The factorial of 0 is 1")
} else {
for(i in 1:num) {
factorial = factorial * i
}
print(paste("The factorial of", num ,"is",factorial))
}
Output:
Enter a number: 8 [1] "The factorial of 8 is 40320"
Example: Find the factorial of a number
# In this program, we input a number check if the number is positive or negative or zero
num = as.double(readline(prompt="Enter a number: "))
if(num > 0) {
print("Positive number")
} else {
if(num == 0) {
print("Zero")
} else {
print("Negative number")
}
}
Output 1
Enter a number: -9.6 [1] "Negative number"
Output 2
Enter a number: 2 [1] "Positive number"
Example: Check to see if a number is positive negative or zero
# In this program, we input a number check if the number is positive or negative or zero
num = as.double(readline(prompt="Enter a number: "))
if(num > 0) {
print("Positive number")
} else {
if(num == 0) {
print("Zero")
} else {
print("Negative number")
}
}
Output 1
Enter a number: -9.6 [1] "Negative number"
Output 2
Enter a number: 2 [1] "Positive number"
Example: Check for Prime Number