x <- c(1,2,-3,4)

if(all(x>0)){ print(“All Postives”) } else{ print(“Not all positives”) }

R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

summary(cars)
##      speed           dist       
##  Min.   : 4.0   Min.   :  2.00  
##  1st Qu.:12.0   1st Qu.: 26.00  
##  Median :15.0   Median : 36.00  
##  Mean   :15.4   Mean   : 42.98  
##  3rd Qu.:19.0   3rd Qu.: 56.00  
##  Max.   :25.0   Max.   :120.00

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.

Logical Expressions in R

This document demonstrates different logical expressions in R and evaluates which one is always FALSE when at least one entry of a logical vector x is TRUE.

Creating a Logical Vector

We define a logical vector x where at least one element is TRUE:

# Define a logical vector where at least one value is TRUE
x <- c(TRUE, FALSE, TRUE, FALSE)
all(x)  # FALSE because not all elements are TRUE
## [1] FALSE
any(x)  # TRUE because at least one element is TRUE
## [1] TRUE
any(!x)  # TRUE because at least one element is FALSE
## [1] TRUE
all(!x)  # FALSE because at least one element is TRUE
## [1] FALSE
# Load built-in dataset containing state names and abbreviations
state_data <- data.frame(name = state.name, abbrev = state.abb)

# Filter state abbreviations where the state name has more than 8 characters
new_names <- state_data$abbrev[nchar(state_data$name) > 8]

# Print the result
new_names
##  [1] "CA" "CT" "LA" "MA" "MN" "MS" "NH" "NJ" "NM" "NC" "ND" "PA" "RI" "SC" "SD"
## [16] "TN" "WA" "WV" "WI"
# Define the function sum_n
sum_n <- function(n) {
  return(sum(1:n))  # Compute the sum of integers from 1 to n
}

# Use the function to compute the sum from 1 to 5000
sum_n(5000)
## [1] 12502500
# Load necessary package for visualization
library(ggplot2)

# Define the altman_plot function
altman_plot <- function(x, y) {
  # Compute sum and difference
  sum_values <- x + y
  diff_values <- x - y
  
  # Create a scatter plot
  ggplot(data = data.frame(Sum = sum_values, Difference = diff_values), aes(x = Sum, y = Difference)) +
    geom_point(color = "blue") +
    labs(title = "Altman Plot: Difference vs. Sum",
         x = "Sum (x + y)",
         y = "Difference (x - y)") +
    theme_minimal()
}

# Example usage
set.seed(123)  # For reproducibility
x_values <- rnorm(100, mean = 50, sd = 10)  # Generate 100 random values
y_values <- rnorm(100, mean = 50, sd = 10)  # Generate another 100 random values

# Call the function to generate the Altman plot
altman_plot(x_values, y_values)

x <- 3
my_func <- function(y){
  x <- 5
  y+5
}
# Define the function compute_s_n
compute_s_n <- function(n) {
  return(sum((1:n)^2))  # Compute the sum of squares from 1 to n
}

# Compute the sum when n = 10
compute_s_n(10)
## [1] 385
# Define an empty numerical vector of size 25
s_n <- vector("numeric", 25)

# Use a for-loop to calculate S_n and store the results in s_n
for (n in 1:25) {
  s_n[n] <- sum((1:n)^2)  # Sum of squares from 1 to n
}

# Display the result
s_n
##  [1]    1    5   14   30   55   91  140  204  285  385  506  650  819 1015 1240
## [16] 1496 1785 2109 2470 2870 3311 3795 4324 4900 5525
# Use sapply to calculate S_n and store the results in s_n
s_n <- sapply(1:25, function(n) sum((1:n)^2))

# Display the result
s_n
##  [1]    1    5   14   30   55   91  140  204  285  385  506  650  819 1015 1240
## [16] 1496 1785 2109 2470 2870 3311 3795 4324 4900 5525
# Load the purrr package
library(purrr)

# Use map_dbl to calculate S_n and store the results in s_n
s_n <- map_dbl(1:25, ~sum((1:.x)^2))

# Display the result
s_n
##  [1]    1    5   14   30   55   91  140  204  285  385  506  650  819 1015 1240
## [16] 1496 1785 2109 2470 2870 3311 3795 4324 4900 5525
# Load the purrr package for map_dbl
library(purrr)

# Calculate S_n using map_dbl (you can use any method like sapply, for-loop, etc.)
s_n <- map_dbl(1:25, ~sum((1:.x)^2))

# Plot S_n versus n
plot(1:25, s_n, type = "p", col = "blue", pch = 19, 
     xlab = "n", ylab = "S_n", 
     main = "Plot of S_n vs n", cex = 1.5)

# Define the formula for the sum of squares
formula_S_n <- function(n) {
  return(n * (n + 1) * (2 * n + 1) / 6)
}

# Calculate S_n using the formula and compare with the computed sum
n_values <- 1:25
computed_s_n <- map_dbl(n_values, ~sum((1:.x)^2))  # Using map_dbl as before
formula_s_n <- formula_S_n(n_values)  # Using the formula

# Compare the two
data.frame(n = n_values, Computed = computed_s_n, Formula = formula_s_n)
##     n Computed Formula
## 1   1        1       1
## 2   2        5       5
## 3   3       14      14
## 4   4       30      30
## 5   5       55      55
## 6   6       91      91
## 7   7      140     140
## 8   8      204     204
## 9   9      285     285
## 10 10      385     385
## 11 11      506     506
## 12 12      650     650
## 13 13      819     819
## 14 14     1015    1015
## 15 15     1240    1240
## 16 16     1496    1496
## 17 17     1785    1785
## 18 18     2109    2109
## 19 19     2470    2470
## 20 20     2870    2870
## 21 21     3311    3311
## 22 22     3795    3795
## 23 23     4324    4324
## 24 24     4900    4900
## 25 25     5525    5525