How to Export a DataFrame to a CSV File in R Programming?

Filed Under: R Programming
Export A Dataframe To Csv File In R

In this tutorial, we are going to export a data frame to CSV file in R. Being a high-end statistical analysis language, R offers tremendous functions and packages, which aids you in the process of data analysis.

Hello and welcome back! In the previous tutorial, we were talked about exporting a data frame to excel using writexl() function. Today, we’re going to export data frames in R to a CSV.


Let’s start with the syntax

write.csv(): R offers the function write.csv, which helps in exporting the data frame to csv file.

write.csv(x,filename,Sep=" ",na="NA",row.names=TRUE)

Where,

  • x = input data frame.
  • Filename = The output file name
  • Sep = The row values will be separated by this symbol.
  • na = Identifies the missing values in the data frame.
  • row.names = Export with / without row names. By default, it will be TRUE.

Let’s create a simple data frame

One of the most basic that we can do is to create a data frame and then export it as a CSV file. Well, in the below code, we are trying to generate a data frame which has 4 columns namely ‘name’,’score’,’gender’ and ‘country’.

We use data.frame function to combine all these columns and data as an organised data frame.

Now, you can see the organised and a good looking data frame in the below output.

#different vectors 
name<-c("Nahida","Ali","Boston","Hubey","Justin","Marcus","Peter","Rossow","Duminy","Roosevelt")

gender<-c("Female","Male","Male","Female","Male","Male","Male","Female","male","Female")

score<-c(89,78,76,56,76,78,87,83,95,61)

country<-c("Argentina","Denmark","USA","Britain","India","Japan","Malaysia","South Korea","Israel","UAE")

#creates a data frame 
df<-data.frame(name,gender,score,country)

#prints the data frame
df
Data Frame In R
Data Frame In R

Now, we have data. The next step will be to export it as a csv file.


Export the data frame to a CSV file

Well, let’s export this data frame to a csv file using write.csv function in R.

The write.csv function works the same as write.xl function. Where the former exports the data as an excel file, the write.csv exports the data as a CSV file.

Let’s export our data as a csv file.

#writes the data frame to a CSV file with the output filename

write.csv(df,"myfirstcsvfile.csv")
exporting data frame to csv in r
CSV file

Now you can see a new CSV file in the above picture. Using the write.csv() function, we can easily export the data frame to a CSV file as shown above.


Export without row names

In the above CSV file, you can see the rows numbered 1,2,3 and so on. We can remove those row names by using the parameter row.names=F. In this section, we are going to export the data without row names.

Let’s consider a previous data fame for this process.

#different vectors 
name<-c("Nahida","Ali","Boston","Hubey","Justin","Marcus","Peter","Rossow","Duminy","Roosevelt")

gender<-c("Femlae","Male","Male","Female","Male","Male","Male","Female","male","Female")

score<-c(89,78,76,56,76,78,87,83,95,61)

country<-c("Argentina","Denmark","USA","Britain","India","Japan","Malaysia","South Korea","Israel","UAE")

#creates a data frame 
df<-data.frame(name,gender,score,country)

#prints the data frame
df

#Export dataframe to CSV file without row names/numbers
write.csv(df,"myfirstcsvfile1.csv",row.names = F)
Write Csv File In R
Writ.csv File In R

We have successfully disabled the row names by passing the parameter row.names=F. In this way you can easily enable or disable the row names in your data. You have to pass TRUE/T to enable and FALSE/F to disable the row names.


Deal with ‘NA’ values in the data

In many of the data sets usually we encounter NA or the missing values. In order to deal with them, we have to pass the argument na=”NA” to the write.csv function.

Let’s import a dateset which has NA values and we can try to export to csv.

Airquality Dataset
Airquality Dataset having NA values

Now, we have airquality dataset where you can see the NA values in multiple columns and rows as well.

Let’s export this data to csv file in R using the function write.csv with the parameter na=”NA” or na=”0″.

#replaces the NA values with 0
write.csv(df,"airqualitydata3.csv",row.names=F,na = "0")
Na In Csv File

In the above csv file, you can see that the function recognises the NA values and replaces them with the numeric ‘0’ as we mentioned. You can replace those missing values by any value of your choice i.e. you can replace with 0 or a with a underscore ‘_’ symbol and more.


Conclusion

R language is best known for its data exploration, processing, manipulation and analysis of the data. It has plenty of functions that assist us in doing so. In this session we have discussed exporting the data frame to a CSV file in R. With the write.csv function, we can easily export data to csv file as shown in the above sections.

The whole process is very simple and takes little time to complete. Keep an eye on the parameters whenever required. That’s all for now.

More study: R documentation

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages