i

R Programming Complete Tutorial

Export data to CSV file

Analysis of external data

This is a complete example of working with an external CSV file. First, we will import the data file from CSV, then check the structure of the data and pre-process the data. After that, we will do some basic data frame operations like creating a data frame from the stored data file and create a subset of data.

Export data to CSV file

In this section, we are going to learn how to export the data frame to a CSV file.  We have to use specifically write.csv() to export the data. In this function, the mandatory arguments are data and file name.


Importing Excel Data

Microsoft Excel is a spreadsheet developed by Microsoft for Windows, macOS, Android, and iOS. Usually, we receive the data set either in CSV format or Excel format. In this section, we will Import Dataset from Excel file using the R default Import Dataset option.  

Step 1: In the initial phase, we have an Excel file with two data sheets ageData and employee, which is available in our current working directory.

Step 2: Now, we will go to the Import Dataset option and select From Excel. If we run this first time, R Studio will install and attach the required libraries.

Step 3: After step 2, we will be landed into the Import Excel Data option. Please follow the below steps to continue:

1. Browse and select the file, which we have planned to work with.

2. Select the worksheet. In our case, we have selected ageData.

3. Now we treat Null values from this NA option. In our worksheet, there were a few ## as Null values. Our NA option will treat them as Null Values.

Finally, we Import the dataset in our R environment. R will store the data in a variable for future work.

Step 4: Now, we will work with the employee datasheet. In this sheet, data is only available in the range B3 to G11. At the time of import this sheet, we have to mention the specific date range.

 

Step 5: Now again, we go to the Import Data option and follow the below actions:

4. Browse and select the file, which we have planned to work with.

5. Select the worksheet. In our case, we have selected employee.

6. We will select the data range B3 to G11 as data is only available in this range.

7. Finally, Import the dataset in the R environment. R will store the data in a variable for future work.

Direct Import from R code:

We can directly import them using the following code snippet.