R studio import excel file




















Second, you will not make typos in the path of your folder path which can sometimes be quite long if you have folders inside folders. Third, when saving your script which I assume you do otherwise you would lose all your work , you also save the actions you just made via the buttons.

So when you reopen your script in the future, no matter what is the current directory, by executing your script which now include the line of code for setting the working directory , you will at the same time specify the working directory you selected for this project.

Now that you have transformed your Excel file into a CSV file and you have specified the folder containing your data by setting the working directory, you are now ready to actually import your dataset. Remind that there are a two methods to import a file:. No matter which method you choose, it is a good practice to first open your file in TextEdit on Mac or Notepad on Windows in order to see the raw data.

If you open the file in Excel you will see the data already formatted and thus miss some important information needed for the importation. Below an example of raw data:. From this window, you can have a preview of your data, and more importantly, check whether your data seems to have been imported correctly. If this is not the case, you can change the import options at the bottom of the window below the data preview corresponding to the information you gathered when looking at the raw data.

Below, the import options you will most likely use:. You should now see your dataset in a new window and from there you can start analyzing your data. This user-friendly method has the advantage that you do not need to remember the code see the next section for the entire code. However, the main drawback is that your import options will not be saved for a future usage so you will need to import your dataset manually each time you open RStudio. Similarly to setting the working directory, I also recommend using the text editor instead of the user-friendly method for the simple reason that you can save your import options when using the text editor and not when using the user-friendly method.

Saving your import options in your script thanks to a line of code allows you to quickly import your dataset the exact same way without having to repeat all the necessary steps every time you import your dataset. The command to import a CSV file is read. Here is an example with the same file than in the user-friendly method:. After the importation you can check whether your data have been correctly imported by running View dat where dat is the name you chose for your data.

A window, similar than for the user-friendly method, will display your data. Alternatively you can also run head dat to see the first 6 rows and check that it corresponds to your Excel file. If something is not correct, edit the import options and check again.

If your dataset has been correctly imported, you can now start analyzing your data. See other articles on R if you want to learn how. The advantage of importing your dataset directly via the code in the text editor is that your import options will be saved for a future usage, preventing you from importing it manually every time you open your script.

You will, however, need to remember the function read. Only Excel files are covered in details here. However, SPSS files. The read. Table of Contents. Improve Article.

Save Article. Like Article. Last Updated : 21 Apr, Previous Plotting a trend graph in Python. Recommended Articles. Article Contributed By :. Easy Normal Medium Hard Expert. To set the correct folder, so to set the working directory equal to the folder where your file is located, follow these steps:.

As you can see in the console, any of the two methods will actually execute the code setwd with the path to the folder you specified. So by clicking on the buttons you actually asked RStudio to write a line of code for you. This method has the advantage that you do not need to remember the code and that you will not make a mistake in the name of the path to your folder. The disadvantage is that if you leave RStudio and open it again later, you will have to specify the working directory again as RStudio did not save your actions via the buttons.

However, you will need to run the command again when reopening RStudio. I recommend this method for several reasons. First, you do not need to remember the setwd function. Second, you will not make typos in the path of your folder path which can sometimes be quite long if you have folders inside folders. Third, when saving your script which I assume you do otherwise you would lose all your work , you also save the actions you just made via the buttons.

So when you reopen your script in the future, no matter what is the current directory, by executing your script which now include the line of code for setting the working directory , you will at the same time specify the working directory you selected for this project. Now that you have transformed your Excel file into a CSV file and you have specified the folder containing your data by setting the working directory, you are now ready to actually import your dataset.

Remind that there are a two methods to import a file:. No matter which method you choose, it is a good practice to first open your file in TextEdit on Mac or Notepad on Windows in order to see the raw data. If you open the file in Excel you will see the data already formatted and thus miss some important information needed for the importation.

Below an example of raw data:. From this window, you can have a preview of your data, and more importantly, check whether your data seems to have been imported correctly.

If this is not the case, you can change the import options at the bottom of the window below the data preview corresponding to the information you gathered when looking at the raw data. Below, the import options you will most likely use:. You should now see your dataset in a new window and from there you can start analyzing your data. This user-friendly method has the advantage that you do not need to remember the code see the next section for the entire code. However, the main drawback is that your import options will not be saved for a future usage so you will need to import your dataset manually each time you open RStudio.

Similarly to setting the working directory, I also recommend using the text editor instead of the user-friendly method for the simple reason that you can save your import options when using the text editor and not when using the user-friendly method. Saving your import options in your script thanks to a line of code allows you to quickly import your dataset the exact same way without having to repeat all the necessary steps every time you import your dataset. The command to import a CSV file is read.

Here is an example with the same file than in the user-friendly method:. After the importation you can check whether your data have been correctly imported by running View dat where dat is the name you chose for your data. A window, similar than for the user-friendly method, will display your data.



0コメント

  • 1000 / 1000