How to convert DataFrame to CSV and Import a CSV File into R in R language.

Often, you’ll work with data in Comma Separated Value (CSV) files. Exporting a DataFrame to CSV and importing a CSV file is an important part of further work/tasks. To start, here are the following steps and genetic syntax that you may use to convert a DataFrame to CSV in the R language.


Sometime one would like to save CSV in a particular location so here this blog also represents how to save a CSV file in a user select location and in the default directory.


There are two ways to import a CSV in R. The first one is you need to use a function read.csv() directly and second you need first install “tidyverse” package and then after use “tidyverse” function read_csv().

To export DataFrame to CSV in R you need to follow these steps: 1. First, you’ll need to create a DataFrame.

2. Pass your DataFrame as a parameter to write.csv() to write your data in CSV file format.

3. CSV files are saved in the default directory but it can also be used to save at a specified location.


4. There is a second method to write the CSV file using write_csv() function. For that pass your DataFrame as a parameter to write_csv().

5. Pass your CSV file as a parameter to read.csv() to read your data.

6. There is a second method to read the CSV file using the read_csv() function. For that pass your CSV file as a parameter to read_csv().


Syntax:


#Export DataFrame to CSV using write.csv() - Writes to a CSV file type
write.csv(DataFrame, 'Path to export the DataFrame\\Filename.csv', row.names = FALSE)

#Export DataFrame to CSV  using write_csv() - Writes to a CSV file type
write_csv(DataFrame, 'Path to export the DataFrame\\Filename.csv')

#Import a CSV file into R using read.csv()- Read a CSV file type
read.csv("Path where your CSV file is located on your computer\\Filename.csv")

#Import a CSV file into R using read_csv() - Read a CSV file type
read_csv("Path where your CSV file is located on your computer\\Filename.csv")

Let’s see how to save a DataFrame as a CSV file using write.csv() method.


Example #1: Save csv to default working directory.


#Create a DataFrame
df <- data.frame(language = c('Python', 'R', 'C++'),
                 extension = c('.py','.R','.cpp'))
print (df)#save the data to a csv_file.
write.csv(df,'language.csv', row.names = FALSE)

Output:



Example #2: You can save your CSV file to a specified location by specifying the path to the Directory.


#Create a DataFrame
df <- data.frame(language = c('Python', 'R', 'C++'),
                 extension = c('.py','.R','.cpp'))
print (df)#save the result on desktop
write.csv(df,'C:\\Users\\user\\Desktop\\language.csv', row.names = FALSE)

Output:


Example #3: You can also save a DataFrame as a CSV file using write_csv() method.


1. Install tidyverse package.

2. Load tidyverse library

3. Use write_csv() function to read the csv file.


install.packages("tidyverse")
library(tidyverse)
write <- write_csv("lang.csv")

Output:


Now, see how to Import a CSV File into R using read.csv() method.


#Create a DataFrame
df <- data.frame(language = c('Python', 'R', 'C++'),
                 extension = c('.py','.R','.cpp'))
print (df)#save the result on desktop
write.csv(df,'C:\\Users\\user\\Desktop\\language.csv', row.names = FALSE)
read.csv('C:\\Users\\user\\Desktop\\language.csv', header = TRUE)

Output:


You can also Import a CSV File into R using read_csv() method.

You need to follow the below steps to read the CSV file using read_csv() function.


1. Install tidyverse package.

2. Load tidyverse library

3. Use read_csv() function to read the csv file.


install.packages("tidyverse")
library(tidyverse)
read1 <-  read_csv("lang.csv")

Run the above commands and then type view(read) so you are able to see the below result.


Output:


Thank You.


177 views0 comments

Recent Posts

See All