R

R#

Hide code cell source
# Install necessary packages if not already installed
if (!requireNamespace("IRkernel", quietly = TRUE)) {
  install.packages("IRkernel", repos = "http://cran.us.r-project.org")
  IRkernel::installspec()
}

if (!requireNamespace("ggplot2", quietly = TRUE)) {
  install.packages("ggplot2", repos = "http://cran.us.r-project.org")
}

if (!requireNamespace("dplyr", quietly = TRUE)) {
  install.packages("dplyr", repos = "http://cran.us.r-project.org")
}

# Load libraries with suppressed messages
suppressPackageStartupMessages(library(ggplot2))
suppressPackageStartupMessages(library(dplyr))

# Print a simple message
print("Hello, Jupyter!")

# Create a simple data frame
data <- data.frame(
  x = rnorm(100),
  y = rnorm(100)
)

# Display the first few rows of the data frame
head(data)

# Create a simple plot
ggplot(data, aes(x = x, y = y)) +
  geom_point() +
  ggtitle("Scatter Plot of Random Data")

# Perform a simple calculation
mean_x <- mean(data$x)
mean_y <- mean(data$y)

# Print the results
cat("Mean of x:", mean_x, "\n")
cat("Mean of y:", mean_y, "\n")
[1] "Hello, Jupyter!"
A data.frame: 6 × 2
xy
<dbl><dbl>
1-0.9227291 1.3450493
2-0.4795123-0.9281464
3-1.6113276 0.4225886
4-0.8332370-0.7399657
5 0.1473884-0.2373443
6-0.5768708 1.0841251
Mean of x: -0.03646671 
Mean of y: -0.197632 
_images/ed36da97c47510c1aa3b56c8748eae826ef54e013dd6489513575b435012411c.png