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 to Create and Manipulate Vectors

Vectors are a fundamental data structure in R. They are used to store a sequence of elements of the same type. Vectors can hold numbers, characters, or logical values.

Creating a vector is straightforward. You use the `c()` function to combine elements into a single vector. For example, `c(1, 2, 3)` creates a numeric vector with three elements.

Vectors support various operations and functions. You can access, modify, and perform calculations directly on them. This flexibility makes vectors essential for data manipulation and analysis in R.

Creating Vectors

Vectors are a basic data type in R. They can be created using the c() function.

# Create a numeric vector
numeric_vector <- c(1, 2, 3, 4, 5)
numeric_vector

Output:

[1] 1 2 3 4 5

Creating Vectors of Different Types

Vectors can also hold characters or logical values.

# Create a character vector
char_vector <- c("apple", "banana", "cherry")
char_vector

# Create a logical vector
logical_vector <- c(TRUE, FALSE, TRUE)
logical_vector

Output for character vector:

[1] "apple"  "banana" "cherry"

Output for logical vector:

[1]  TRUE FALSE  TRUE

Accessing Elements

You can access vector elements using square brackets.

# Access the second element
numeric_vector[2]

Output:

[1] 2

Modifying Elements

Change elements by assigning new values.

# Modify the third element
numeric_vector[3] <- 10
numeric_vector

Output:

[1]  1  2 10  4  5

Vector Operations

Perform operations directly on vectors.

# Add 5 to each element
numeric_vector + 5

Output:

[1]  6  7 15  9 10

Combining Vectors

Combine vectors with the c() function.

# Combine two vectors
combined_vector <- c(numeric_vector, char_vector)
combined_vector

Output:

[1] "1"      "2"      "10"     "4"      "5"      "apple"  "banana" "cherry"

Using Functions with Vectors

Apply functions like length() and mean() to vectors.

# Get the length of a vector
length(numeric_vector)

# Calculate the mean of a numeric vector
mean(numeric_vector)

Output for length:

[1] 5

Output for mean:

[1] 6.2
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 Examples
Up Next
R Code to Work with Data Frames
  • About Us
  • Contact Us
  • Archive
  • Hindi
  • Tamil
  • Telugu
  • Marathi
  • Gujarati
  • Malayalam
  • Kannada
  • Privacy Policy
  • Copyright 2025 Happiom. All Rights Reserved.