VOOZH about

URL: https://www.geeksforgeeks.org/r-language/how-to-create-matrix-and-vector-from-csv-file-in-r/

⇱ How to create matrix and vector from CSV file in R ? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to create matrix and vector from CSV file in R ?

Last Updated : 23 Jul, 2025

In this article, we will discuss how to convert CSV data into a matrix and a vector in R Programming Language. We will use read.csv() function to load the csv file:

Syntax: object=read.csv(path)

where, path is the location of a file present in our local system.

Matrix: Matrix is a two-dimensional data structure that contains rows and columns. It can hold multiple data types. We can convert csv file data into matrix by using the method called as.matrix()

Syntax:as.matrix(csv_file_object)

Vector: Vector is a one-dimensional data structure that can hold multiple datatypes. We can convert CSV data into a vector, By using as.vector()

Syntax: as.vector(csv_file_object)

CSV File Used:

👁 Image

Step 1: Create an object to CSV by reading the path

Output:

 Name ID
1 sravan 7058
2 Jyothika 7059

Step 2: Convert the data into a matrix.

Output:

 Name ID
[1, ] "sravan" "7058"
[2, ] " Jyothika" "7059"

Step 3: Convert the data into a vector

Output:

 Name ID
1 sravan 7058
2 Jyothika 7059

Below is the full implementation:

Output:

 Name ID
[1, ] "sravan" "7058"
[2, ] " Jyothika" "7059"
 Name ID
1 sravan 7058
2 Jyothika 7059
Comment
Article Tags:

Explore