Open In App

How to convert CSV into array in R?

Last Updated : 02 Jul, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we are going to see how to convert CSV into an array in R Programming Language. If we load this file into any environment like python, R language .etc, the data will be displayed in rows and columns format, let us convert CSV file into an array. Each and every column in the CSV will be converted into array of multiple dimensions.

Method 1 : Using loop.

Here we will use loop to convert all the columns in to the array. 

R




setwd("C:/Users/KRISHNA KARTHIKEYA/Documents")
 
df = read.csv("item.csv"
lst1 = list() 
for(i in 1:ncol(df)) {     
  lst1[[i]] <- df[ , i]   
}
 
names(lst1) = colnames(df) 
arr = array(unlist(lst1),
            dim = c(5, 5, 3))
print(arr)


Output:

Explanation :

  • In the first step we changed the directory using setwd() function to where the csv file is located.
  • In the next step we have imported a CSV file into R environment using read.csv() function.
  • Then created a empty list.
  • Using for loop extracted every element by column wise and passed to list() function.
  • Converted list to array using array() by passing list variable as parameter. In the array() function we have used unlist() function. The unlist() function is used to convert list to vector. So, the unlist() with list variable is passed to array().
  • Finally, printed the array variable.

Method 2: Extract every column into separate variable and pass to array() function.

R




setwd('C:/Users/KRISHNA KARTHIKEYA/Documents')
 
df = read.csv('item.csv')
a = df$id
b = df$item
c = df$quantity
d = df$price
e = df$bought
f = df$forenoon
g = df$afternoon
arr = array(c(a, b, c, d, e, f, g),
            dim = c(5, 3 ,2))
print(arr)


Output:

Explanation :

  • In the first step we changed the directory using setwd() function to where the csv file is located.
  • In the next step we have imported a CSV file into R environment using read.csv() function.
  • Using $ operator we have extracted every column into separate variable.
  • Pass all the column variables to array( ) and give dimensions using dim attribute.
  • Finally, printed the array variable.


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
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
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
How to Read a CSV from URL into R?
In this article, we are going to see how to read CSV files from URL using R Programming Language. Method 1: Using Base R Here we are using read.csv() methods, which is an inbuilt function in R programming. This function is similar to Python because it read the CSV file and returns the data into dataframe. Syntax: read.csv(URL) Example: Read a CSV f
1 min read
Convert CSV to list in R
In this article, we will discuss how to convert the content of the CSV file to list in R Programming Language. CSV Used: Method 1 : Using for loop In this method, the file is first read into the R program and then one by one using for loop the columns are extracted and converted to list explicitly using list() function. Example : C/C++ Code df=read
1 min read
How to Convert a CSV File to Microsoft Excel in R
CSV refers to Comma-Separated Values. It holds plain text as a series of values (cells) separated by commas (, ) in a series of lines (rows). CSV file can actually open in a text editor and read it. On the other hand, Excel is used to display the data in horizontal and vertical rows. The data are usually stored in the cells. We have an option of fo
2 min read
How to create matrix and vector from CSV file in R ?
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 ca
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 Export DataFrame to CSV in R ?
R Programming language allows us to read and write data into various files like CSV, Excel, XML, etc. In this article, we are going to discuss how to Export DataFrame to CSV file in R Programming Language. Approach: Write Data in column wise formatCreate DataFrame for these dataWrite Data to the CSV filePrint the success message Function used: data
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
Article Tags :