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.3268388 0.4903918
2 0.3605232 0.6668099
3 0.9607080-1.2184292
4-1.1387228 0.7133311
5-0.4677493 1.6855603
6-0.7356920-0.8246378
Mean of x: 0.0844 
Mean of y: 0.1018842 
_images/74572eefb4f0dc230013ef581e346864652dd702326dd6f0f09a8445971eba1e.png