HappiomHappiom
  • Self-Improvement
  • Relationship
  • AI for Life
  • Apps
  • Tech
  • More
    • Online Diary
    • Glossary
  • Learn
    • Book
    • >> Soft Skills
    • Time Management
    • >> Tech Skills
    • R
    • Linux
    • Python
  • Our Apps
    • Download Diary App
    • Write Your First Diary
    • Login to Online Diary App
    • 100K+ Famous Quotes Site
  • Resources
    • Self-Improvement Guide
      • 21-Days to Self-Improvement
      • Creating a Habit
      • Learn Life Experiences
      • Easily Prioritizing Tasks
      • Learning from Mistakes
      • Doing Regular Exercises
      • Setting Priority for Success
      • Avoiding Common Mistakes
      • Eating Healthy Food Regularly
    • Journaling Guide
      • Online Diary
      • Best Diary Apps
      • Diary Writing Ideas
      • Diary Writing Topics
      • Avoid Writing in Diary
      • Diary Writing as Hobby
      • Reasons to Write a Diary
      • Types of Feelings In Diary
      • Improve Diary Writing Skills
  • Self-Improvement
  • Relationship
  • AI for Life
  • Apps
  • Tech
  • More
    • Online Diary
    • Glossary
  • Learn
    • Book
    • >> Soft Skills
    • Time Management
    • >> Tech Skills
    • R
    • Linux
    • Python
  • Our Apps
    • Download Diary App
    • Write Your First Diary
    • Login to Online Diary App
    • 100K+ Famous Quotes Site
  • Resources
    • Self-Improvement Guide
      • 21-Days to Self-Improvement
      • Creating a Habit
      • Learn Life Experiences
      • Easily Prioritizing Tasks
      • Learning from Mistakes
      • Doing Regular Exercises
      • Setting Priority for Success
      • Avoiding Common Mistakes
      • Eating Healthy Food Regularly
    • Journaling Guide
      • Online Diary
      • Best Diary Apps
      • Diary Writing Ideas
      • Diary Writing Topics
      • Avoid Writing in Diary
      • Diary Writing as Hobby
      • Reasons to Write a Diary
      • Types of Feelings In Diary
      • Improve Diary Writing Skills
Expand All Collapse All
  • R Code Examples
    • R Code to Create and Manipulate Vectors
    • R Code to Work with Data Frames
    • R Code to Handle Factors and Categorical Data
    • Example R Code for Basic Data Visualization with ggplot2
    • R Code to Aggregate Data Using dplyr
    • R Code to Apply Functions with lapply and sapply
    • R Code to Handle Missing Data
    • Example R Code for String Manipulation with stringr
    • R Code to Transform Data with tidyr
    • R Code to Perform ADF Test
    • R Code to Perform Data Import and Export with CSV
    • R Code for Filtering Data
    • R Code for Easily Summarizing Data
    • R Code to Perform Linear Regression for Statistical Analysis
    • R Code to Perform t-tests for Statistical Analysis
    • Example R Code for Time Series Analysis
    • R Code for Doing Web Scraping with Examples
    • R Code to Showcase Geospatial Analysis
    • Example R Code to Filter Multiple Conditions (for Data Manipulation)

R Code for Easily Summarizing Data

In data analysis, summarizing the key characteristics of a dataset is essential for understanding its underlying patterns and making informed decisions. R, a powerful programming language and environment for statistical computing and graphics, provides a range of functions to summarize data efficiently.

I’ll write a detailed R code for summarizing data. I’ll use a dataset called iris which is built into R for this example.

The code below summarizes the data by calculating mean, median, standard deviation, and quartiles for each numerical variable in the dataset.

# Load the iris dataset
data(iris)

# Display the first few rows of the dataset
head(iris)

# Summary statistics for each numerical variable in the dataset
summary(iris)

# Mean of each numerical variable
mean_values <- sapply(iris[,1:4], mean)

# Median of each numerical variable
median_values <- sapply(iris[,1:4], median)

# Standard deviation of each numerical variable
sd_values <- sapply(iris[,1:4], sd)

# Quartiles of each numerical variable
quartiles <- t(sapply(iris[,1:4], quantile, probs = c(0.25, 0.5, 0.75)))

# Create a dataframe to store summary statistics
summary_df <- data.frame(
  Variable = colnames(iris[,1:4]),
  Mean = mean_values,
  Median = median_values,
  SD = sd_values,
  Q1 = quartiles[,1],
  Q3 = quartiles[,3]
)

# Display the summary dataframe
summary_df

This code will give you a detailed summary of the iris dataset, including mean, median, standard deviation, and quartiles for each numerical variable (Sepal.Length, Sepal.Width, Petal.Length, and Petal.Width).

You can replace iris with your own dataset and adjust the code accordingly if you’re working with a different dataset.

You can use the above code in any R environment where you’re working with data analysis or statistical computations.

Let’s see few examples where you might use this code.

  • You can open a new script file in RStudio, paste the code there, and run it using the “Run” button or by selecting the code and pressing `Ctrl+Enter` (Windows/Linux) or `Cmd+Enter` (Mac).
  • If you prefer working directly in the R command line interface, you can paste the code line by line or as a whole and press `Enter` to run each line.
  • If you’re working on a report or analysis document using RMarkdown, you can include this code in a code chunk within your RMarkdown document and knit it to generate a report containing the summary statistics.
  • You can save the code in a plain text file with a `.R` extension and then run the script from the command line or by sourcing it within an R session using `source(“path/to/your/script.R”)`.
  • If you’re using Jupyter Notebook with an R kernel, you can create a new notebook, add a code cell, paste the code, and execute the cell to see the output.

These are just a few examples of where you can use the provided code. Depending on your workflow and preferences, you can choose the environment that suits you best.

R programming is always interesting to work with!

Related Articles
  • R Code to Transform Data with tidyr
  • Example R Code for String Manipulation with stringr
  • R Code to Handle Missing Data
  • R Code to Apply Functions with lapply and sapply
  • R Code to Aggregate Data Using dplyr
  • Example R Code for Basic Data Visualization with ggplot2

No luck finding what you need? Contact Us

Previously
R Code for Filtering Data
Up Next
R Code to Perform Linear Regression for Statistical Analysis
  • About Us
  • Contact Us
  • Archive
  • Hindi
  • Tamil
  • Telugu
  • Marathi
  • Gujarati
  • Malayalam
  • Kannada
  • Privacy Policy
  • Copyright 2025 Happiom. All Rights Reserved.