Importing and Exporting Data in R Studio

In this post i am going to talk about importing common data format like Excel/CSV files or Text files in R studio and also how you can export the data out of R studio to Excel,CSV and Text file formats.

CSV Files

The utils package, which is automatically loaded in R studio on startup, can import CSV files using read.csv function. To import CSV data files we can do it in the following ways:

1) file <- read.csv(file.choose())

This will initiate a pop-up window to open and you can manually choose the desired CSV file. It will create a dataframe named "file".

The other important arguments to consider in the above statement are header=True when the CSV file contains header (column names). The default for this argument is always true.

Also, an important argument to specify is separator (sep= ). You have to specify the character that is separating the fields. "," means data is separated by comma.


2) file <- read.csv("file-name.csv",header=,sep=)

This statement works the same way as above and only difference is you have to specify the .csv filename instead of choosing the file from a popup window.

3) Importing csv file using the readr package

In the previous two methods you have used read.csv function which is a part of utils package. You can also do the same job using another package called "readr".

To load and initiate the package in the Rstudio:

install.packages("readr")
library(readr) 

After loading and calling for the package you can import the csv file as follows:

file <- read_csv("file-name.csv")

As you can see the only difference is using read_csv instead of read.csv as you used previously. 

Excel Files

To import an excel file with .xlsx or .xls extension , we can make use of readxl package

install.packages("readxl")
library(readxl)

Your excel file may contain many sheets or Tabs. You can view the names of each excel sheets in your excel file by :

excel_sheets("name-of-your-excel-file.xlsx") 

You can read the sheets one by one and import them as dataframe by :

my_data <- read_excel("name-of-your-excel-file.xlsx", sheet=1)

my_data1 <- read_excel("name-of-your-excel-file.xlsx",sheet=2)

Also explore other packages : XLConnect, Xlsx, gdata

Text Files

If you have a .txt or a tab-delimited text file, you can easily import it with basic R fucntion read.table(). This function is a part of utils package which is automatically loaded in the Rstudio on startup.

my_data <- read,table("yourdatafile.txt", sep='\t", header=T)

sep=\t is for tab delimited file . Similarly for csv file sep=","

Exporting Data out of RStudio 

Text Files

You can export your dataframes out of Rstudio to a text file by using write.table function.

write.table(name-of-your-dataframe,"path-of-your-filename",sep="\t")

CSV Files

write.csv(my-df,"path-of-your-desired-csvfile.csv")

Excel Files

You can export your dataframes to an excel file (.xlsx or .xls) by using write_xlsx function.

install.packages("writexl")

library(writexl)

write_xlsx(my-df,"filepath.xlsx")



Comments

Popular posts from this blog

Data Structure in R