Converting an Excel File to an R file: Optimizing File Size
Today, I will introduce a method for converting an Excel file into an R file. I have placed an Excel file in a folder named ‘DataBase’ on the desktop. This file contains wheat grain size data, with 96,320 rows and a size of approximately 15MB. When an Excel file is large, you may experience performance issues, such as Excel slowing down during data operations, especially if your computer has limited memory. It would be more convenient to convert this Excel file into an R file and work with it in R, not only to reduce the file size but also for smoother data operations.
I have uploaded the data to my GitHub. If you copy and paste the code below into your R console, you can import the data into R.
library(readr)
github="https://raw.githubusercontent.com/agronomy4future/raw_data_practice/main/wheat_grains_data_training.csv"
dataA=data.frame(read_csv(url(github),show_col_types= FALSE))
The data has been uploaded to R. Now, I will convert this data into an R file and save it with the name “dataB.”
save(dataA, file="C:/Users/Desktop/dataB.RData")
** Check the pathway in your PC
I will now verify if the R file has been successfully saved. The file “dataB.RData” has been stored on my desktop. However, the file size has decreased to 601 KB. When saving the same data to an Excel file, it becomes approximately 5 MB in size.
You can now store a much smaller file size on your computer.
Now, let’s proceed to load this file back into R.
load("C:/Users/Desktop/dataB.RData", verbose=T)
The reason for including the verbose=T
code is to remind us of the original name of the file that was loaded. When you execute the code, it uploads the R data and explicitly designates the original data name as ‘dataA,’ indicating that the data is uploaded to R with the original name ‘dataA’.
If the data in the Excel file has undergone all necessary calculations and requires no further processing, it’s much more convenient to save it as an R file rather than an Excel file. This not only saves on file size but also facilitates working with the data in R.