This tutorial will show you how to import an excel file into the R programming language.
Importing an Excel File into R
Below are the ways to import an Excel File into R:
Method #1: Using read_excel() Function
Installing package into ‘C:/Users/cirus/AppData/Local/R/win-library/4.2’ (as ‘lib’ is unspecified) --- Please select a CRAN mirror for use in this session --- trying URL 'https://cloud.r-project.org/bin/windows/contrib/4.2/readxl_1.4.0.zip' Content type 'application/zip' length 1197616 bytes (1.1 MB) downloaded 1.1 MB package ‘readxl’ successfully unpacked and MD5 sums checked The downloaded binary packages are in C:\Users\cirus\AppData\Local\Temp\Rtmpo5UGo0\downloaded_packages
The read_excel() method is used to read/import an Excel file and is only accessible after the readxl library has been imported into the R program.
read_excel(filepath, sheet, dtype = "float32")
- filepath: It is the name of the input file to be read or imported
- sheet: It is the sheet name in the Excel file.
- dtype: It is the Numpy data type.
The variable is treated to be a data frame.
- Import the readxl package using the library() function.
- Pass the file path to be read as an argument to the read_excel() function and store it in a variable.
- Here it reads/imports the given excel file.
- Display the data of the above given excel file.
- The Exit of the Program.
Below is the implementation:
# Import the readxl package using the library() function library(readxl) # Pass the file path to be read as an argument to the read_excel() function # and store it in a variable. # Here it reads/imports the given excel file gvn_excelfile <- read_excel("C:/Users/cirus/Downloads/excelFile.xlsx") # Display the data of the above given excel file gvn_excelfile
# A tibble: 12 × 10 `Website name` Name Age City State Student Employed Married <chr> <chr> <dbl> <chr> <chr> <chr> <chr> <chr> 1 Sheets Tips Vikram 21 Hyderabad Tela… Yes No No 2 Python-Programs Ashritha 22 Pune Mumb… Yes No No 3 BTechGeeks Manish 19 Indore Madh… Yes No No 4 PythonArray Anish 20 Rajkot Guja… Yes No No 5 Sheets Tips Vishal 18 Ranchi Jhar… Yes No No 6 Python-Programs Pavan 23 Lucknow Utta… No Yes No 7 Sheets Tips John 32 Los Angeles Amer… No Yes Yes 8 Python-Programs Virat 34 Agra Delhi No Yes No 9 BTechGeeks Cary 36 Tokyo Japan No Yes Yes 10 PythonArray Tara 39 Helsinki Finl… No Yes Yes 11 Sheets Tips Smith 42 Nairobi Kenya No Yes Yes 12 Python-Programs Elizabeth 66 Paris Italy No Yes Yes # … with 2 more variables: Scholorship <chr>, Id <dbl>
Method #2: Using the built-in menu Options of Rstudio
This method is simpler than the previous one for importing an excel file in R because it is the only one that does not require the user to type any code in the console to import the excel file.
Moreover, the user only needs to operate on the environment window of the studio.
Steps to Import an Excel File Using the Dataset Option in the environment window of Rstudio:
Step#1: In the environment window, click the Import Dataset option. The user must now click the option to import the dataset from the environment window of Rstudio
Under the Import Dataset option, click “From Excel” option. Here, the user must select “from excel” because the file is in the form of an excel spreadsheet under the import dataset option to import the excel file.
Select the Browse option and click on the excel file you want to import. Now, by selecting the Browse option, the user will be able to select the required Excel file to be imported into R. The user must then select the required Excel file to import into R.
Step#4: Click on the import option and the excel file is successfully imported. In this final step, the user must click the import button, which will result in the successful importing of the selected excel file into R.