R OBJECTS

Dr. Ajay Kumar Koli, PhD | SARA Institute of Data Science, India

R Object

  • In R, everything you create or work with is stored as an object.

  • Objects can be numbers, text, data tables, functions, or even plots.

  • Think of an object as a named container that stores information in your R environment.

Common Types of R Objects

  1. Vector – a sequence of values (same type)
numbers <- c(1, 2, 3, 4, 5)     # Numeric vector

names <- c("Asha", "Raj", "Zara")  # Character vector

names
[1] "Asha" "Raj"  "Zara"


Curious Candy 🍬🍭🍬

Why are we not seeing numbers in the results?

Common Types of R Objects

  1. Matrix – 2D table of numbers
mymatrix <- matrix(1:6, nrow = 2)

mymatrix
     [,1] [,2] [,3]
[1,]    1    3    5
[2,]    2    4    6

Common Types of R Objects

  1. Data Frame – table with rows and columns (like Excel)
mydata <- data.frame(
  Name = c("Ali", "Beena"),
  Age = c(25, 30)
)


mydata
   Name Age
1   Ali  25
2 Beena  30

Common Types of R Objects

  1. List – collection of different types of objects

    • A list can contain vectors, numbers, strings, even other lists.
mylist <- list(name = "Sara", scores = c(90, 85, 88), passed = TRUE)

mylist
$name
[1] "Sara"

$scores
[1] 90 85 88

$passed
[1] TRUE

Common Types of R Objects

  1. Function – reusable set of instructions
add <- function(x, y) {
  return(x + y)
}

add(3, 4)   # Output: 7
[1] 7

💡Guidelines to “name” R Objects:

  • A name cannot start with a number.

  • A name cannot use some special symbols, like ^, !, $, @, +, -, /, or *,:.

  • Avoid caps.

  • Avoid space.

  • Use dash (like weight-kg) or underscore (like weight_kg).

  • If chronology matters then add date (2020-09-05-file-name).

🤯 Your Turn: Create this dataset

🤩 Your Turn Answer

Create Object

  • Code
  • Output
age <- c(23, 25, 16, 40, 34)

age

[1] 23 25 16 40 34

Assignment Operator


Important

R assignment operators: Assignment operators are used to assign values to variables.

object_name <- c(2, 3, 4, 5)

object_name

Create object

  • Code
  • Output
income <- c(23000, 25000, 16000, 4000, 34000)

income

[1] 23000 25000 16000 4000 34000

Create object

  • Code
  • Output
name <- c("Bhim", "Rama", "Sara", "Phule", "Savitri")

name

[1] “Bhim” “Rama” “Sara” “Phule” “Savitri”

Create object

  • Code
  • Output
place <- c("MH", "RJ", "DL", "HR", "HR")

place

[1] “MH” “RJ” “DL” “HR” “HR”

Create object

  • Code
  • Output
weight_kg <- c(50, 52, 61, 40, 70)

weight_kg

[1] 50 52 61 40 70

RStudio Environment Window


Basic Object Types


Type Description Example
numeric Numbers (floating point) 3.14, 2, -5
integer Whole numbers 2L, 100L
character Text strings "R is great"
logical Boolean values TRUE, FALSE

🤔 How to combine all these objects and form a data set?

👇 Something Like This 😻😻


     name income age place weight_kg
1    Bhim  23000  23    MH        50
2    Rama  25000  25    RJ        52
3    Sara  16000  16    DL        61
4   Phule   4000  40    HR        40
5 Savitri  34000  34    HR        70

How to create a data object?

  • Code
  • Output
example_df <- data.frame(name, income, age, place, weight_kg)

example_df
     name income age place weight_kg
1    Bhim  23000  23    MH        50
2    Rama  25000  25    RJ        52
3    Sara  16000  16    DL        61
4   Phule   4000  40    HR        40
5 Savitri  34000  34    HR        70

Export data as a csv file

  • Code
  • Output
library(readr)

# create a folder in wd & name it "data"
write_csv(example_df, "data/example_df.csv") 

To see the created file, check the “data” folder in your working directory.

List of all objects

  • Code
  • Output
objects()

[1] “add” “age” “example_df” “income” “mydata”
[6] “mylist” “mymatrix” “name” “names” “numbers”
[11] “place” “weight_kg”

R Comments


Artwork by Alision Horst

Comment:

  • Writing comments in R code (or any programming language) is essential for making your code understandable, maintainable, and collaborative.

  • Comments are lines in the code that are not executed; they’re used to explain what the code does, why certain decisions were made, and how to use or modify the code.

How to Write Comments in R

  • Use # to begin a comment.

  • R ignores everything after the # on that line.

# This is a comment

x <- 5  # This sets x to 5

Why Comments Are Important in R

  1. Improves Readability
    • Helps you and others understand the logic quickly, especially after some time.
  2. Supports Collaboration
    • Team members can follow your logic and contribute more easily.
  3. Eases Debugging and Maintenance
    • Clear comments make it easier to find and fix issues or update code.
  4. Documents Assumptions and Logic
    • Explains why certain steps are performed in a specific way.

Examples

🛑 Bad Practice: No Comments

```{r}
#| eval: false

a <- read.csv("data.csv")
b <- a[a$score > 80, ]
c <- mean(b$score)
```

🙁 This code works, but it’s not clear what it’s doing or why.

✅ Better Version With Comments

```{r}
#| eval: false

# Read data from CSV file
data <- read.csv("data.csv")

# Filter rows where score is greater than 80
high_scores <- data[data$score > 80, ]

# Calculate average of the high scores
avg_score <- mean(high_scores$score)
```

RStats
Community


Artwork by Alision Horst

Help using console >

in console type ?your query

  • for example ?knitr.

  • for example ?mtcars.

  • for example ?dplyr.

RStudio: Package website


Posit community

Stack Overflow

GitHub

Social media #RStats

Artwork by Alision Horst

1 / 34
R OBJECTS Dr. Ajay Kumar Koli, PhD | SARA Institute of Data Science, India

  1. Slides

  2. Tools

  3. Close
  • R OBJECTS
  • R Object
  • Common Types of R Objects
  • Common Types of R Objects
  • Common Types of R Objects
  • Common Types of R Objects
  • Common Types of R Objects
  • 💡Guidelines to “name” R Objects:
  • 🤯 Your Turn: Create this dataset
  • 🤩 Your Turn Answer
  • Create Object
  • Assignment Operator
  • Create object
  • Create object
  • Create object
  • Create object
  • RStudio Environment Window
  • Basic Object Types
  • 🤔 How to combine all these objects and form a data set?
  • How to create a data object?
  • Export data as a csv file
  • List of all objects
  • R Comments
  • Comment:
  • How to Write Comments in R
  • Why Comments Are Important in R
  • Examples
  • RStats Community
  • Help using console >
  • RStudio: Package website
  • Posit community
  • Stack Overflow
  • GitHub
  • Social media #RStats
  • f Fullscreen
  • s Speaker View
  • o Slide Overview
  • e PDF Export Mode
  • r Scroll View Mode
  • ? Keyboard Help