The Wayback Machine - https://web.archive.org/web/20240725073108/https://www.geeksforgeeks.org/how-to-create-matrix-and-vector-from-csv-file-in-r/
Open In App

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

Last Updated : 21 Apr, 2021
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

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:

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

R




data=read.csv("C:/sravan/data.csv")
print(data)


Output:

    Name       ID
1  sravan    7058
2  Jyothika  7059

Step 2: Convert the data into a matrix.

R




matrixdata = as.matrix(data)
print(matrixdata)


Output:

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

Step 3: Convert the data into a vector

R




vectordata=as.vector(data)
print(vectordata)


Output:

    Name       ID
1  sravan    7058
2  Jyothika  7059

Below is the full implementation:

R




# Read data from CSV
data=read.csv("C:/sravan/data.csv")
  
# Create a matrix
matrixdata=as.matrix(data)
  
# Create a vector
vectordata=as.vector(data)
print(matrixdata)
print(vectordata)


Output:

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


Similar Reads

Read contents of a CSV File in R Programming - read.csv() Function
read.csv() function in R Language is used to read "comma separated value" files. It imports data in the form of a data frame. Syntax: read.csv(file, header, sep, dec) Parameters: file: the path to the file containing the data to be imported into R. header: logical value. If TRUE, read.csv() assumes that your file has a header row, so row 1 is the n
3 min read
Create a Vector of Colors from a Vector of Gray Levels in R Programming - gray() or grey() Function
gray() or grey() function in R Language is used to create a vector of colors from a vector of gray levels. Syntax: gray(level) or grey(level) Parameters: level: a vector of desired gray levels between 0 and 1; zero indicates "black" and one indicates "white". Example 1: # R program to illustrate # gray function # Calling the gray() function gray(0)
1 min read
How to create a new vector from a given vector in R
In this article, we will discuss How to create a new vector from a given vector in R Programming Language. Create a new vector from a given vectorYou can use various functions and techniques depending on your needs to create a new vector from a given vector in R. Here are some common methods. 1. Subset the VectorIf you want to create a new vector w
2 min read
Read CSV file and select specific rows and columns in R
In this article, we are going to see how to read CSV file and select specific rows and columns in R Programming Language. CSV file: To import a CSV file into the R environment we need to use a pre-defined function called read.csv(). Pass filename.csv as a parameter within quotations. First, we need to set the path to where the CSV file is located u
1 min read
How to plot a graph in R using CSV file ?
To plot a graph in R using a CSV file, we need a CSV file with two-column, the values in the first column will be considered as the points at the x-axis and the values in the second column will be considered as the points at the y-axis. In this article, we will be looking at the way to plot a graph using a CSV file in R language. Approach Import cs
2 min read
How to calculate mean of a CSV file in R?
Mean or average is a method to study central tendency of any given numeric data. It can be found using the formula. [Tex]Mean= (sum of data)/(frequency of data)[/Tex] In this article, we will be discussing two different ways to calculate the mean of a CSV file in R. Data in use: Method 1: Using mean function In this method to calculate the mean of
2 min read
Reading the CSV file into Dataframes in R
In this article, we will learn how to import or read a CSV file into a dataframe in R Programming Language. Data set in use: Step 1: Set or change the working directory In order to import or read the given CSV file into our data frame, we first need to check our current working directory, and make sure that the CSV file is in the same directory as
3 min read
Export CSV File without Row Names in R
In this article, we will learn how to export CSV files without row names in R Programming Language. In R language we use write.csv() function to create a CSV file from the data. Syntax: write.csv(df, path) Parameters: df: dataframe objectpath: local path on your system where .csv file will be written/saved. For this, we have to create a dataframe w
1 min read
Export List to CSV or text File in R
In this article, we will discuss how to export a list to CSV and text file using R Programming language. Exporting to both of these types of files can be done using capture.output() function. This function is mainly used to evaluate its arguments with the output being returned as a character string or sent to a file. By just changing the file name
2 min read
How to Import a CSV File into R ?
A CSV file is used to store contents in a tabular-like format, which is organized in the form of rows and columns. The column values in each row are separated by a delimiter string. The CSV files can be loaded into the working space and worked using both in-built methods and external package imports. Method 1: Using read.csv() method The read.csv()
3 min read
three90RightbarBannerImg